Skip to contentSkip to conversation

arm64: Realtek RTD1295 RTC

PATCH Superseded Andreas Färber 3 participants 15 messages Aug 27, 2017 – Aug 28, 2017

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover arm64: Realtek RTD1295 RTC

Hello,

This series adds the RTC for the Realtek RTD1295 SoC. Based on my RTD1295 clk series.

There being no public source code for RTD1295, the implementation is based on register offsets seen in the vendor DT, as well as older mach-rtk119x code published by QNAP.

The base year is still hardcoded in v2. Calculations changed.

The DT node depends on the clk series for clock index and header.

More experimental patches at:
https://github.com/afaerber/linux/commits/rtd1295-next

Have a lot of fun!

Cheers,
Andreas

v1 -> v2:
* Updated rtc driver to no longer use open/release (Alexandre)
* Cleaned up debug output (Andrew)
* Avoided COMPILE_TEST division errors (kbuild)
* Various cleanups and extensions

Cc Alessandro Zummo <[email protected]>
Cc Alexandre Belloni <[email protected]>
Cc linux-rtc at vger.kernel.org
Cc Roc He <[email protected]>
Cc devicetree at vger.kernel.org
Cc Andrew Lunn <[email protected]>

Andreas F?rber (3):
dt-bindings: rtc: Add Realtek RTD1295
rtc: Add Realtek RTD1295
arm64: dts: realtek: Add RTD1295 RTC node

 .../devicetree/bindings/rtc/realtek,rtd119x.txt    |  16 ++
 arch/arm64/boot/dts/realtek/rtd1295.dtsi           |   6 +
 drivers/rtc/Kconfig                                |   8 +
 drivers/rtc/Makefile                               |   1 +
 drivers/rtc/rtc-rtd119x.c                          | 254 +++++++++++++++++++++
 5 files changed, 285 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/realtek,rtd119x.txt
 create mode 100644 drivers/rtc/rtc-rtd119x.c
Signature · 1 line
2.12.3
#1 lore raw

PATCH v2 1/3 dt-bindings: rtc: Add Realtek RTD1295

Add a binding for the RTC on the Realtek RTD119x/RTD129x SoC families.

---
v1 -> v2: Unchanged

 .../devicetree/bindings/rtc/realtek,rtd119x.txt          | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/realtek,rtd119x.txt
Documentation/devicetree/bindings/rtc/realtek,rtd119x.txt new file +16 -0
new file mode 100644
index 000000000000..bbf1ccb5df31
@@ -0,0 +1,16 @@
+Realtek RTD129x Real-Time Clock
+===============================
+
+Required properties:
+- compatible :  Should be "realtek,rtd1295-rtc"
+- reg        :  Specifies the physical base address and size
+- clocks     :  Specifies the clock gate
+
+
+Example:
+
+	rtc at 9801b600 {
+		compatible = "realtek,rtd1295-clk";
+		reg = <0x9801b600 0x100>;
+		clocks = <&clkc RTD1295_CLK_EN_MISC_RTC>;
+	};
Signature · 1 line
2.12.3
#2 lore raw

PATCH v2 2/3 rtc: Add Realtek RTD1295

In reply to Andreas Färber

Based on QNAP's arch/arm/mach-rtk119x/driver/rtk_rtc_drv.c code and mach-rtk119x/driver/dc2vo/fpga/include/mis_reg.h register definitions.

The base year 2014 was observed on all of Zidoo X9S, ProBox2 Ava and Beelink Lake I.

---
v1 -> v2:
* Dropped open/release in favor of probe/remove (Alexandre)
* read_time: Reordered register accesses (Alexandre)
* read_time/set_time: Refactored day calculations to avoid time64_t (Alexandre)
* read_time: Retry if seconds change (Alexandre)
* probe: Added missing RTCACR initialization code
* set_time: Fixed year check (off by 1900)
* set_time: Fixed new seconds (off by factor two)
* Cleaned up debug output (Andrew)
* Added spinlocks around register accesses
* Added masks for register fields

 drivers/rtc/Kconfig       |   8 ++
 drivers/rtc/Makefile      |   1 +
 drivers/rtc/rtc-rtd119x.c | 254 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 263 insertions(+)
 create mode 100644 drivers/rtc/rtc-rtd119x.c
drivers/rtc/Kconfig +8 -0
index 22efa21b1d81..d5a46f311ecb 100644
@@ -1765,6 +1765,14 @@ config RTC_DRV_CPCAP
 	   Say y here for CPCAP rtc found on some Motorola phones
 	   and tablets such as Droid 4.
 
+config RTC_DRV_RTD119X
+	bool "Realtek RTD129x RTC"
+	depends on ARCH_REALTEK || COMPILE_TEST
+	default ARCH_REALTEK
+	help
+	  If you say yes here, you get support for the RTD1295 SoC
+	  Real Time Clock.
+
 comment "HID Sensor RTC drivers"
 
 config RTC_DRV_HID_SENSOR_TIME
drivers/rtc/Makefile +1 -0
index acd366b41c85..55a0a5ca45b0 100644
@@ -131,6 +131,7 @@ obj-$(CONFIG_RTC_DRV_RP5C01)	+= rtc-rp5c01.o
 obj-$(CONFIG_RTC_DRV_RS5C313)	+= rtc-rs5c313.o
 obj-$(CONFIG_RTC_DRV_RS5C348)	+= rtc-rs5c348.o
 obj-$(CONFIG_RTC_DRV_RS5C372)	+= rtc-rs5c372.o
+obj-$(CONFIG_RTC_DRV_RTD119X)	+= rtc-rtd119x.o
 obj-$(CONFIG_RTC_DRV_RV3029C2)	+= rtc-rv3029c2.o
 obj-$(CONFIG_RTC_DRV_RV8803)	+= rtc-rv8803.o
 obj-$(CONFIG_RTC_DRV_RX4581)	+= rtc-rx4581.o
drivers/rtc/rtc-rtd119x.c new file +254 -0

254 lines not on this page · raw message

Signature · 1 line
2.12.3
#3 lore raw

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

Quoted 101 lines · Andreas F?rber

Based on QNAP's arch/arm/mach-rtk119x/driver/rtk_rtc_drv.c code and mach-rtk119x/driver/dc2vo/fpga/include/mis_reg.h register definitions.

The base year 2014 was observed on all of Zidoo X9S, ProBox2 Ava and Beelink Lake I.

---
v1 -> v2:
* Dropped open/release in favor of probe/remove (Alexandre)
* read_time: Reordered register accesses (Alexandre)
* read_time/set_time: Refactored day calculations to avoid time64_t (Alexandre)
* read_time: Retry if seconds change (Alexandre)
* probe: Added missing RTCACR initialization code
* set_time: Fixed year check (off by 1900)
* set_time: Fixed new seconds (off by factor two)
* Cleaned up debug output (Andrew)
* Added spinlocks around register accesses
* Added masks for register fields

 drivers/rtc/Kconfig       |   8 ++
 drivers/rtc/Makefile      |   1 +
 drivers/rtc/rtc-rtd119x.c | 254 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 263 insertions(+)
 create mode 100644 drivers/rtc/rtc-rtd119x.c
drivers/rtc/Kconfig +8 -0
index 22efa21b1d81..d5a46f311ecb 100644
@@ -1765,6 +1765,14 @@ config RTC_DRV_CPCAP
 	   Say y here for CPCAP rtc found on some Motorola phones
 	   and tablets such as Droid 4.
 
+config RTC_DRV_RTD119X
+	bool "Realtek RTD129x RTC"
+	depends on ARCH_REALTEK || COMPILE_TEST
+	default ARCH_REALTEK
+	help
+	  If you say yes here, you get support for the RTD1295 SoC
+	  Real Time Clock.
+
 comment "HID Sensor RTC drivers"
 
 config RTC_DRV_HID_SENSOR_TIME
drivers/rtc/Makefile +1 -0
index acd366b41c85..55a0a5ca45b0 100644
@@ -131,6 +131,7 @@ obj-$(CONFIG_RTC_DRV_RP5C01)	+= rtc-rp5c01.o
 obj-$(CONFIG_RTC_DRV_RS5C313)	+= rtc-rs5c313.o
 obj-$(CONFIG_RTC_DRV_RS5C348)	+= rtc-rs5c348.o
 obj-$(CONFIG_RTC_DRV_RS5C372)	+= rtc-rs5c372.o
+obj-$(CONFIG_RTC_DRV_RTD119X)	+= rtc-rtd119x.o
 obj-$(CONFIG_RTC_DRV_RV3029C2)	+= rtc-rv3029c2.o
 obj-$(CONFIG_RTC_DRV_RV8803)	+= rtc-rv8803.o
 obj-$(CONFIG_RTC_DRV_RX4581)	+= rtc-rx4581.o
drivers/rtc/rtc-rtd119x.c new file +38 -0
new file mode 100644
index 000000000000..27fa68a5af30
@@ -0,0 +1,254 @@
+/*
+ * Realtek RTD129x RTC
+ *
+ * Copyright (c) 2017 Andreas F?rber
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <linux/clk.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/spinlock.h>
+
+#define RTD_RTCSEC		0x00
+#define RTD_RTCMIN		0x04
+#define RTD_RTCHR		0x08
+#define RTD_RTCDATE1		0x0c
+#define RTD_RTCDATE2		0x10
+#define RTD_RTCACR		0x28
+#define RTD_RTCEN		0x2c
+#define RTD_RTCCR		0x30
+
+#define RTD_RTCSEC_RTCSEC_MASK		0x7f
+
+#define RTD_RTCMIN_RTCMIN_MASK		0x3f
+
+#define RTD_RTCHR_RTCHR_MASK		0x1f
+
+#define RTD_RTCDATE1_RTCDATE1_MASK	0xff
+
+#define RTD_RTCDATE2_RTCDATE2_MASK	0x7f
+
+#define RTD_RTCACR_RTCPWR		BIT(7)
+

drivers/rtc/rtc-rtd119x.c line 48

Quoted 10 lines
drivers/rtc/rtc-rtd119x.c new file +10 -0
+#define RTD_RTCEN_RTCEN_MASK		0xff
+
+#define RTD_RTCCR_RTCRST		BIT(6)
+
+struct rtd119x_rtc {
+	void __iomem *base;
+	struct clk *clk;
+	struct rtc_device *rtcdev;
+	unsigned base_year;
+	spinlock_t lock;

Where is this lock initialised? I would expect a call to spin_lock_init() somewhere.

I also wonder what this lock is protecting, which rtc->ops_lock does not protect?

Andrew

#4 lore raw

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

Am 27.08.2017 um 04:05 schrieb Andrew Lunn:

Quoted 10 lines

drivers/rtc/rtc-rtd119x.c line 48

Quoted 6 lines · Andreas F?rber
drivers/rtc/rtc-rtd119x.c new file +6 -0
+struct rtd119x_rtc {
+	void __iomem *base;
+	struct clk *clk;
+	struct rtc_device *rtcdev;
+	unsigned base_year;
+	spinlock_t lock;

Where is this lock initialised? I would expect a call to spin_lock_init() somewhere.

Hm, the spinlock in my irq mux series doesn't have that call either; my reset driver did have it. The zero initialization appears to work OK, but you're probably right that it should be there.

Quoted 2 lines

I also wonder what this lock is protecting, which rtc->ops_lock does not protect?

By that same argument we could ask why so many drivers (and mine, too) are calling rtc_valid_tm() when __rtc_read_time() calls it again...

The downstream code is locking inside individual functions such as check_acr or set_enabled; I adopted it for whole operations only, but you're right that so far it matches the RTC class ops granularity, so I can drop it again. The latching discussion had reminded me of locking.

Cheers,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 N?rnberg, Germany
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
#5 lore raw

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

Quoted 12 lines · Andreas F?rber

Am 27.08.2017 um 04:05 schrieb Andrew Lunn:

Quoted 10 lines

drivers/rtc/rtc-rtd119x.c line 48

Quoted 6 lines · Andreas F?rber
drivers/rtc/rtc-rtd119x.c new file +6 -0
+struct rtd119x_rtc {
+	void __iomem *base;
+	struct clk *clk;
+	struct rtc_device *rtcdev;
+	unsigned base_year;
+	spinlock_t lock;

Where is this lock initialised? I would expect a call to spin_lock_init() somewhere.

Quoted 3 lines

Hm, the spinlock in my irq mux series doesn't have that call either; my reset driver did have it. The zero initialization appears to work OK, but you're probably right that it should be there.

Hi Andreas

I suspect you will have problems if you enable spin lock debug code, like CONFIG_DEBUG_SPINLOCK.

Andrew

#6 lore raw

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

Hi Andrew,

Am 27.08.2017 um 05:27 schrieb Andrew Lunn:

Quoted 13 lines · Andreas F?rber
Quoted 12 lines

Am 27.08.2017 um 04:05 schrieb Andrew Lunn:

Quoted 10 lines

drivers/rtc/rtc-rtd119x.c line 48

Quoted 6 lines · Andreas F?rber
drivers/rtc/rtc-rtd119x.c new file +6 -0
+struct rtd119x_rtc {
+	void __iomem *base;
+	struct clk *clk;
+	struct rtc_device *rtcdev;
+	unsigned base_year;
+	spinlock_t lock;

Where is this lock initialised? I would expect a call to spin_lock_init() somewhere.

Quoted 8 lines
Quoted 3 lines

Hm, the spinlock in my irq mux series doesn't have that call either; my reset driver did have it. The zero initialization appears to work OK, but you're probably right that it should be there.

Hi Andreas

I suspect you will have problems if you enable spin lock debug code, like CONFIG_DEBUG_SPINLOCK.

Thanks for that hint. Surprisingly the irq mux is still fine with that option enabled, but the rtc now shows:

[    1.193722] BUG: spinlock bad magic on CPU#0, swapper/0/1
[    1.199264]  lock: 0xffff80007bd40db8, .magic: 00000000, .owner:
<none>/-1, .owner_cpu: 0
[    1.207648] CPU: 0 PID: 1 Comm: swapper/0 Not tainted
4.13.0-rc6-next-20170825-00061-g88513c6f5687 #180
[    1.217269] Hardware name: Zidoo X9S (DT)
[    1.221378] Call trace:
[    1.223900] [<ffff0000082878f4>] dump_backtrace+0x0/0x320
[    1.229439] [<ffff000008287c28>] show_stack+0x14/0x1c
[    1.234622] [<ffff00000875c6ac>] dump_stack+0x94/0xbc
[    1.239804] [<ffff0000082eba4c>] spin_bug+0x84/0xa4
[    1.244806] [<ffff0000082ebaf8>] do_raw_spin_lock+0x30/0xd4
[    1.250522] [<ffff000008770868>] _raw_spin_lock_irqsave+0x28/0x38
[    1.256772] [<ffff00000864cc14>] rtd119x_rtc_read_time+0x24/0x14c
[    1.263019] [<ffff00000864918c>] __rtc_read_time+0x3c/0x64
[    1.268644] [<ffff0000086491e8>] rtc_read_time+0x34/0x5c
[    1.274091] [<ffff000008649cc8>] __rtc_read_alarm+0x24/0x2f0
[    1.279893] [<ffff000008648b3c>] rtc_device_register+0xa0/0x144
[    1.285962] [<ffff000008648d88>] devm_rtc_device_register+0x5c/0xa4
[    1.292390] [<ffff00000864d038>] rtd119x_rtc_probe+0x174/0x1b4
[    1.298373] [<ffff000008555038>] platform_drv_probe+0x54/0xa8
[    1.304265] [<ffff0000085537dc>] driver_probe_device+0x1fc/0x2b8
[    1.310423] [<ffff00000855390c>] __driver_attach+0x74/0xa4
[    1.316051] [<ffff000008551d58>] bus_for_each_dev+0x80/0x90
[    1.321765] [<ffff000008553198>] driver_attach+0x20/0x28
[    1.327211] [<ffff000008552d8c>] bus_add_driver+0x194/0x1e0
[    1.332925] [<ffff000008554234>] driver_register+0x98/0xd0
[    1.338550] [<ffff000008554f90>] __platform_driver_register+0x48/0x50
[    1.345155] [<ffff0000089924f0>] rtd119x_rtc_driver_init+0x18/0x20
[    1.351493] [<ffff000008283284>] do_one_initcall+0x118/0x13c
[    1.357299] [<ffff000008970dd0>] kernel_init_freeable+0x220/0x224
[    1.363548] [<ffff00000876c128>] kernel_init+0x10/0xf8
[    1.368819] [<ffff000008284200>] ret_from_fork+0x10/0x18
[    1.375104] rtd1295-rtc 9801b600.rtc: rtc core: registered rtc as rtc0

Will fix.

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 N?rnberg, Germany
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
#7 lore raw

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

In reply to Andreas Färber
Quoted 16 lines · Andreas F?rber

Am 27.08.2017 um 04:05 schrieb Andrew Lunn:

Quoted 10 lines

drivers/rtc/rtc-rtd119x.c line 48

Quoted 6 lines · Andreas F?rber
drivers/rtc/rtc-rtd119x.c new file +6 -0
+struct rtd119x_rtc {
+	void __iomem *base;
+	struct clk *clk;
+	struct rtc_device *rtcdev;
+	unsigned base_year;
+	spinlock_t lock;

Where is this lock initialised? I would expect a call to spin_lock_init() somewhere.

Hm, the spinlock in my irq mux series doesn't have that call either; my reset driver did have it. The zero initialization appears to work OK, but you're probably right that it should be there.

Quoted 6 lines
Quoted 2 lines

I also wonder what this lock is protecting, which rtc->ops_lock does not protect?

By that same argument we could ask why so many drivers (and mine, too) are calling rtc_valid_tm() when __rtc_read_time() calls it again...

Most of them probably predates the introduction of the check in __rtc_read_time().

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

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

Am 27.08.2017 um 10:27 schrieb Alexandre Belloni:

Quoted 7 lines · Andreas F?rber
Quoted 3 lines

Am 27.08.2017 um 04:05 schrieb Andrew Lunn:
By that same argument we could ask why so many drivers (and mine, too) are calling rtc_valid_tm() when __rtc_read_time() calls it again...

Most of them probably predates the introduction of the check in __rtc_read_time().

Replacing with 0 here then.

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 N?rnberg, Germany
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
#9 lore raw

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

In reply to Andreas Färber

Hi,

Not much to add, apart from the spinlock issue already spotted by Andrew.

drivers/rtc/rtc-rtd119x.c line 47

Quoted 5 lines · Andreas F?rber
drivers/rtc/rtc-rtd119x.c new file +5 -0
+struct rtd119x_rtc {
+	void __iomem *base;
+	struct clk *clk;
+	struct rtc_device *rtcdev;
+	unsigned base_year;

checkpatch complains this should be made unsigned int

drivers/rtc/rtc-rtd119x.c line 53

Quoted 6 lines
drivers/rtc/rtc-rtd119x.c new file +6 -0
+	spinlock_t lock;
+};
+
+static inline int rtd119x_rtc_year_days(int year)
+{
+	return rtc_year_days(1, 12, year);

I'm not sure it is worth wrapping rtc_year_days

drivers/rtc/rtc-rtd119x.c line 90

Quoted 7 lines
drivers/rtc/rtc-rtd119x.c new file +7 -0
+static int rtd119x_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+	struct rtd119x_rtc *data = dev_get_drvdata(dev);
+	unsigned long flags;
+	s32 day;
+	u32 sec;
+	unsigned year;

unsigned int

Quoted 32 lines
drivers/rtc/rtc-rtd119x.c new file +32 -0
+	int tries = 0;
+
+	spin_lock_irqsave(&data->lock, flags);
+
+ while (true) {
+     tm->tm_sec = (readl_relaxed(data->base + RTD_RTCSEC) & RTD_RTCSEC_RTCSEC_MASK) >> 1;
+     tm->tm_min  = readl_relaxed(data->base + RTD_RTCMIN) & RTD_RTCMIN_RTCMIN_MASK;
+     tm->tm_hour = readl_relaxed(data->base + RTD_RTCHR) & RTD_RTCHR_RTCHR_MASK;
+     day  =  readl_relaxed(data->base + RTD_RTCDATE1) & RTD_RTCDATE1_RTCDATE1_MASK;
+     day |= (readl_relaxed(data->base + RTD_RTCDATE2) & RTD_RTCDATE2_RTCDATE2_MASK) << 8;
+     sec  = (readl_relaxed(data->base + RTD_RTCSEC) & RTD_RTCSEC_RTCSEC_MASK) >> 1;
+     tries++;
+
+     if (sec == tm->tm_sec)
+         break;
+
+     if (tries >= 3) {
+         spin_unlock_irqrestore(&data->lock, flags);
+         return -EINVAL;
+     }
+ }
+ if (tries > 1)
+     dev_dbg(dev, "%s: needed %i tries\n", __func__, tries);
+
+ spin_unlock_irqrestore(&data->lock, flags);
+
+ year = data->base_year;
+ while (day >= rtd119x_rtc_year_days(year)) {
+     day -= rtd119x_rtc_year_days(year);
+     year++;
+ }
+ tm->tm_year = year - 1900;

drivers/rtc/rtc-rtd119x.c line 132

Quoted 10 lines
drivers/rtc/rtc-rtd119x.c new file +10 -0
+ tm->tm_yday = day;
+
+ tm->tm_mon = 0;
+ while (day >= rtc_month_days(tm->tm_mon, year)) {
+     day -= rtc_month_days(tm->tm_mon, year);
+     tm->tm_mon++;
+ }
+ tm->tm_mday = day + 1;
+
+ return rtc_valid_tm(tm);

As you spotted, you can return 0 here.

drivers/rtc/rtc-rtd119x.c line 139

Quoted 7 lines
drivers/rtc/rtc-rtd119x.c new file +7 -0
+}
+
+static int rtd119x_rtc_set_time(struct device *dev, struct rtc_time *tm)
+{
+	struct rtd119x_rtc *data = dev_get_drvdata(dev);
+	unsigned long flags;
+	unsigned day;

ditto

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

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

Hi Alexandre,

Am 27.08.2017 um 11:13 schrieb Alexandre Belloni:

Quoted 10 lines

Not much to add, apart from the spinlock issue already spotted by Andrew.

Review

Quoted 5 lines · Andreas F?rber
(quoted diff) +5 -0
+struct rtd119x_rtc {
+	void __iomem *base;
+	struct clk *clk;
+	struct rtc_device *rtcdev;
+	unsigned base_year;

checkpatch complains this should be made unsigned int

Ouch, I forgot to add my pre-commit hook in this tree and wasn't aware of that rule yet. The RFC had it already. Fixed.

Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	spinlock_t lock;
+};
+
+static inline int rtd119x_rtc_year_days(int year)
+{
+	return rtc_year_days(1, 12, year);

I'm not sure it is worth wrapping rtc_year_days

[snip]

Well, I found your rtc_year_days rather confusing and had to play with the arguments until I got it working as expected, so I wanted an inline function (or macro) as abstraction from my three callers.

Sadly the naming is rather confusing as I am looking for the number of days 365..366, whereas your rtc_year_days is meant to return 0..365 and I would just like to extract the 12th array element but need to counter the -1 subtraction. rtc_year_days(31, 11, year) + 1 is not intuitive either - reads like November (and ranges are not documented).

What about exporting a convenient rtc_days_in_year(year) from rtc-lib.c accessing the table directly without rtc_year_days detour? Alternatively an inline function in rtc.h to the same effect without the array?

Also despite is_leap_year() returning a bool || expression you keep using it as array index or integer to add. That assumes true == 1, whereas to my knowledge only false is guaranteed to be 0 and any non-zero value means true. So I'd expect the code to be like this:

drivers/rtc/rtc-lib.c +0 -1
index 1ae7da5cfc60..8983a408fc30 100644
@@ -32,7 +32,7 @@ static const unsigned short rtc_ydays[2][13] = {
  */
 int rtc_month_days(unsigned int month, unsigned int year)
 {
-       return rtc_days_in_month[month] + (is_leap_year(year) && month

== 1);
+ return rtc_days_in_month[month] + ((is_leap_year(year) && month
== 1) ? 1 : 0);
}
EXPORT_SYMBOL(rtc_month_days);

@@ -41,7 +41,7 @@ EXPORT_SYMBOL(rtc_month_days);
*/
int rtc_year_days(unsigned int day, unsigned int month, unsigned int year) {
- return rtc_ydays[is_leap_year(year)][month] + day-1;
+ return rtc_ydays[is_leap_year(year) ? 1 : 0][month] + day-1;
}
EXPORT_SYMBOL(rtc_year_days);

@@ -69,7 +69,7 @@ void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)

                - LEAPS_THRU_END_OF(1970 - 1);
        if (days < 0) {
                year -= 1;
-               days += 365 + is_leap_year(year);
+               days += 365 + (is_leap_year(year) ? 1 : 0);
        }
        tm->tm_year = year - 1900;
        tm->tm_yday = days + 1;

The above rtc_time64_to_tm() hunk could be converted to the proposed rtc_days_in_year(). rtc-mcp795.c has another candidate.

By reusing rtc_year_days I elegantly avoided is_leap_year in my code, but I could spell out 365 + (is_leap_year(year) ? 1 : 0) in my function if preferred. I dislike duplicating expressions in code.

What do you think?

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 N?rnberg, Germany
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
#11 lore raw

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

Quoted 10 lines
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+static inline int rtd119x_rtc_year_days(int year)
+{
+	return rtc_year_days(1, 12, year);

I'm not sure it is worth wrapping rtc_year_days

[snip]

Well, I found your rtc_year_days rather confusing and had to play with the arguments until I got it working as expected, so I wanted an inline function (or macro) as abstraction from my three callers.

I agree with that. I was wondering why 1st December was being used. I would say this API does not do too well on Rusty's API Design Manifesto.

It does at least get the day/month/year in the right order ;-)

Andrew

#12 lore raw

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

Quoted 7 lines · Andrew Lunn
Quoted 7 lines
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+static inline int rtd119x_rtc_year_days(int year)
+{
+	return rtc_year_days(1, 12, year);

I'm not sure it is worth wrapping rtc_year_days

[snip]

Quoted 8 lines
Quoted 3 lines

Well, I found your rtc_year_days rather confusing and had to play with the arguments until I got it working as expected, so I wanted an inline function (or macro) as abstraction from my three callers.

I agree with that. I was wondering why 1st December was being used. I would say this API does not do too well on Rusty's API Design Manifesto.

It follows the convention that tm_mon is 0-11. Making it easy to read for anyone used to the RTC subsystem. I can agree it is not the most intuitive one. That choice predates the RTC subsystem (made in 1996), I won't give any name ;)

Quoted 3 lines

It does at least get the day/month/year in the right order ;-)

Andrew

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

Re: [PATCH v2 2/3] rtc: Add Realtek RTD1295

In reply to Andreas Färber

Hi,

Quoted 10 lines · Andreas F?rber

Well, I found your rtc_year_days rather confusing and had to play with the arguments until I got it working as expected, so I wanted an inline function (or macro) as abstraction from my three callers.

Sadly the naming is rather confusing as I am looking for the number of days 365..366, whereas your rtc_year_days is meant to return 0..365 and I would just like to extract the 12th array element but need to counter the -1 subtraction. rtc_year_days(31, 11, year) + 1 is not intuitive either - reads like November (and ranges are not documented).

Quoted 4 lines

What about exporting a convenient rtc_days_in_year(year) from rtc-lib.c accessing the table directly without rtc_year_days detour? Alternatively an inline function in rtc.h to the same effect without the array?

This could have been done but what you did in your v3 is fine too. It will always be time to move that to the core later.

Quoted 4 lines

Also despite is_leap_year() returning a bool || expression you keep using it as array index or integer to add. That assumes true == 1, whereas to my knowledge only false is guaranteed to be 0 and any non-zero value means true. So I'd expect the code to be like this:

sizeof(bool) (actually _Bool) is 1 so it can only be 0 or 1.

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

PATCH v2 3/3 arm64: dts: realtek: Add RTD1295 RTC node

In reply to Andreas Färber

Add RTC node to the Realtek RTD1295 Device Tree.

---
v1 -> v2: Unchanged

Depends on the pending clock bindings.

 arch/arm64/boot/dts/realtek/rtd1295.dtsi | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm64/boot/dts/realtek/rtd1295.dtsi +6 -0
index 503e2d5fc334..8ae0949ad89e 100644
@@ -192,6 +192,12 @@
 			status = "disabled";
 		};
 
+		rtc at 9801b600 {
+			compatible = "realtek,rtd1295-rtc";
+			reg = <0x9801b600 0x100>;
+			clocks = <&clkc RTD1295_CLK_EN_MISC_RTC>;
+		};
+
 		gic: interrupt-controller at ff011000 {
 			compatible = "arm,gic-400";
 			reg = <0xff011000 0x1000>,
Signature · 1 line
2.12.3