* [PATCH v2 0/2] Enable RTC for the MT6357
@ 2025-04-02 10:50 Alexandre Mergnat
2025-04-02 10:50 ` [PATCH v2 1/2] rtc: mt6359: add mt6357 support Alexandre Mergnat
2025-04-02 10:51 ` [PATCH v2 2/2] rtc: mt6397: Fix mt6357 RTC year offset handling for hwclock commands Alexandre Mergnat
0 siblings, 2 replies; 5+ messages in thread
From: Alexandre Mergnat @ 2025-04-02 10:50 UTC (permalink / raw)
To: Eddie Huang, Sean Wang, Alexandre Belloni, Matthias Brugger,
AngeloGioacchino Del Regno, Macpaul Lin
Cc: linux-arm-kernel, linux-mediatek, linux-rtc, linux-kernel,
Alexandre Mergnat
MT6357 PMIC's RTC use can use the MT6397 drivers with the MT6358 data.
Also, since kernel 6.6, a regression has been observed on the hwclock
command. More detail on the commit messages.
Signed-off-by: Alexandre Mergnat <amergnat@baylibre.com>
---
Changes in v2:
- Split the patch to have:
- Add MT6357 support
- Fix hwclock issue
- Handle the year offset in another way, but the V1 way still viable.
- Link to v1: https://lore.kernel.org/r/20250109-enable-rtc-v1-0-e8223bf55bb8@baylibre.com
---
Alexandre Mergnat (2):
rtc: mt6359: add mt6357 support
rtc: mt6397: Fix mt6357 RTC year offset handling for hwclock commands
drivers/rtc/rtc-mt6397.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
---
base-commit: 97274527e8dc709bbb4c7cb44279a12d085da9ef
change-id: 20250109-enable-rtc-b2ff435af2d5
Best regards,
--
Alexandre Mergnat <amergnat@baylibre.com>
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v2 1/2] rtc: mt6359: add mt6357 support 2025-04-02 10:50 [PATCH v2 0/2] Enable RTC for the MT6357 Alexandre Mergnat @ 2025-04-02 10:50 ` Alexandre Mergnat 2025-04-02 10:51 ` [PATCH v2 2/2] rtc: mt6397: Fix mt6357 RTC year offset handling for hwclock commands Alexandre Mergnat 1 sibling, 0 replies; 5+ messages in thread From: Alexandre Mergnat @ 2025-04-02 10:50 UTC (permalink / raw) To: Eddie Huang, Sean Wang, Alexandre Belloni, Matthias Brugger, AngeloGioacchino Del Regno, Macpaul Lin Cc: linux-arm-kernel, linux-mediatek, linux-rtc, linux-kernel, Alexandre Mergnat The MT6357 PMIC contains the same RTC as MT6358 which allows to add support for it trivially by just complementing the list of compatibles. Signed-off-by: Alexandre Mergnat <amergnat@baylibre.com> --- drivers/rtc/rtc-mt6397.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c index 6979d225a78e4..692c00ff544b2 100644 --- a/drivers/rtc/rtc-mt6397.c +++ b/drivers/rtc/rtc-mt6397.c @@ -332,6 +332,7 @@ static const struct mtk_rtc_data mt6397_rtc_data = { static const struct of_device_id mt6397_rtc_of_match[] = { { .compatible = "mediatek,mt6323-rtc", .data = &mt6397_rtc_data }, + { .compatible = "mediatek,mt6357-rtc", .data = &mt6358_rtc_data }, { .compatible = "mediatek,mt6358-rtc", .data = &mt6358_rtc_data }, { .compatible = "mediatek,mt6397-rtc", .data = &mt6397_rtc_data }, { } -- 2.25.1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] rtc: mt6397: Fix mt6357 RTC year offset handling for hwclock commands 2025-04-02 10:50 [PATCH v2 0/2] Enable RTC for the MT6357 Alexandre Mergnat 2025-04-02 10:50 ` [PATCH v2 1/2] rtc: mt6359: add mt6357 support Alexandre Mergnat @ 2025-04-02 10:51 ` Alexandre Mergnat 2025-04-02 13:03 ` AngeloGioacchino Del Regno 1 sibling, 1 reply; 5+ messages in thread From: Alexandre Mergnat @ 2025-04-02 10:51 UTC (permalink / raw) To: Eddie Huang, Sean Wang, Alexandre Belloni, Matthias Brugger, AngeloGioacchino Del Regno, Macpaul Lin Cc: linux-arm-kernel, linux-mediatek, linux-rtc, linux-kernel, Alexandre Mergnat The mt6357 RTC was failing when using the `hwclock -r --verbose` command, despite reading correctly through sysfs. There is high chance for other platform to be impacted by the year offset handling issue. The hardware RTC registers store years relative to 1968, but the driver wasn't consistently applying the offset when converting between hardware and Linux time representation. This inconsistency caused alarm rollover failures during device registration, with the error "alarm rollover not handled -22" in the logs, causing hwclock commands to fail. The ioctl interface used by the hwclock command requires proper time range validation that wasn't happening with the inconsistent year offsets. Fixes the issue by applying the year offset in all operations: - Adding (RTC_MIN_YEAR - RTC_BASE_YEAR) when reading from hardware - Subtracting the same offset when writing to hardware - Using the same logic for both regular time and alarm operations With these changes, the hwclock command works correctly and time values are consistently handled across all interfaces. Signed-off-by: Alexandre Mergnat <amergnat@baylibre.com> --- drivers/rtc/rtc-mt6397.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c index 692c00ff544b2..ba52e225dc8fa 100644 --- a/drivers/rtc/rtc-mt6397.c +++ b/drivers/rtc/rtc-mt6397.c @@ -77,7 +77,8 @@ static int __mtk_rtc_read_time(struct mt6397_rtc *rtc, tm->tm_mday = data[RTC_OFFSET_DOM]; tm->tm_wday = data[RTC_OFFSET_DOW]; tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_TC_MTH_MASK; - tm->tm_year = data[RTC_OFFSET_YEAR]; + /* The RTC registers store years since 1968 (hardware's base year) */ + tm->tm_year = data[RTC_OFFSET_YEAR] + (RTC_MIN_YEAR - RTC_BASE_YEAR); ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC, sec); exit: @@ -119,7 +120,8 @@ static int mtk_rtc_set_time(struct device *dev, struct rtc_time *tm) data[RTC_OFFSET_DOM] = tm->tm_mday; data[RTC_OFFSET_DOW] = tm->tm_wday; data[RTC_OFFSET_MTH] = tm->tm_mon; - data[RTC_OFFSET_YEAR] = tm->tm_year; + /* Convert from tm_year (years since 1900) to RTC register format (years since 1968) */ + data[RTC_OFFSET_YEAR] = tm->tm_year - (RTC_MIN_YEAR - RTC_BASE_YEAR); mutex_lock(&rtc->lock); ret = regmap_bulk_write(rtc->regmap, rtc->addr_base + RTC_TC_SEC, @@ -165,8 +167,8 @@ static int mtk_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK; tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK; tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK; - tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK; - + /* Apply the same offset conversion for alarm read */ + tm->tm_year = (data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK) + (RTC_MIN_YEAR - RTC_BASE_YEAR); tm->tm_mon--; return 0; @@ -200,8 +202,9 @@ static int mtk_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) (tm->tm_mday & RTC_AL_DOM_MASK)); data[RTC_OFFSET_MTH] = ((data[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK)) | (tm->tm_mon & RTC_AL_MTH_MASK)); + /* Convert alarm year using the same offset as in read/write time */ data[RTC_OFFSET_YEAR] = ((data[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK)) | - (tm->tm_year & RTC_AL_YEA_MASK)); + ((tm->tm_year - (RTC_MIN_YEAR - RTC_BASE_YEAR)) & RTC_AL_YEA_MASK)); if (alm->enabled) { ret = regmap_bulk_write(rtc->regmap, -- 2.25.1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] rtc: mt6397: Fix mt6357 RTC year offset handling for hwclock commands 2025-04-02 10:51 ` [PATCH v2 2/2] rtc: mt6397: Fix mt6357 RTC year offset handling for hwclock commands Alexandre Mergnat @ 2025-04-02 13:03 ` AngeloGioacchino Del Regno 2025-04-11 14:20 ` Alexandre Mergnat 0 siblings, 1 reply; 5+ messages in thread From: AngeloGioacchino Del Regno @ 2025-04-02 13:03 UTC (permalink / raw) To: Alexandre Mergnat, Eddie Huang, Sean Wang, Alexandre Belloni, Matthias Brugger, Macpaul Lin Cc: linux-arm-kernel, linux-mediatek, linux-rtc, linux-kernel Il 02/04/25 12:51, Alexandre Mergnat ha scritto: > The mt6357 RTC was failing when using the `hwclock -r --verbose` command, > despite reading correctly through sysfs. There is high chance for other > platform to be impacted by the year offset handling issue. > > The hardware RTC registers store years relative to 1968, but the driver > wasn't consistently applying the offset when converting between > hardware and Linux time representation. > > This inconsistency caused alarm rollover failures during device > registration, with the error "alarm rollover not handled -22" in the > logs, causing hwclock commands to fail. > > The ioctl interface used by the hwclock command requires proper time > range validation that wasn't happening with the inconsistent year > offsets. > > Fixes the issue by applying the year offset in all operations: > - Adding (RTC_MIN_YEAR - RTC_BASE_YEAR) when reading from hardware > - Subtracting the same offset when writing to hardware > - Using the same logic for both regular time and alarm operations > > With these changes, the hwclock command works correctly and time > values are consistently handled across all interfaces. > > Signed-off-by: Alexandre Mergnat <amergnat@baylibre.com> > --- > drivers/rtc/rtc-mt6397.c | 13 ++++++++----- > 1 file changed, 8 insertions(+), 5 deletions(-) > > diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c > index 692c00ff544b2..ba52e225dc8fa 100644 > --- a/drivers/rtc/rtc-mt6397.c > +++ b/drivers/rtc/rtc-mt6397.c > @@ -77,7 +77,8 @@ static int __mtk_rtc_read_time(struct mt6397_rtc *rtc, > tm->tm_mday = data[RTC_OFFSET_DOM]; > tm->tm_wday = data[RTC_OFFSET_DOW]; > tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_TC_MTH_MASK; > - tm->tm_year = data[RTC_OFFSET_YEAR]; > + /* The RTC registers store years since 1968 (hardware's base year) */ > + tm->tm_year = data[RTC_OFFSET_YEAR] + (RTC_MIN_YEAR - RTC_BASE_YEAR); This patch received a NACK because of RTC_MIN_YEAR_OFFSET. What you're doing here is avoiding to use the "RTC_MIN_YEAR_OFFSET" definition name but otherwise doing the very same thing that was NACKed before. Regards, Angelo > > ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC, sec); > exit: > @@ -119,7 +120,8 @@ static int mtk_rtc_set_time(struct device *dev, struct rtc_time *tm) > data[RTC_OFFSET_DOM] = tm->tm_mday; > data[RTC_OFFSET_DOW] = tm->tm_wday; > data[RTC_OFFSET_MTH] = tm->tm_mon; > - data[RTC_OFFSET_YEAR] = tm->tm_year; > + /* Convert from tm_year (years since 1900) to RTC register format (years since 1968) */ > + data[RTC_OFFSET_YEAR] = tm->tm_year - (RTC_MIN_YEAR - RTC_BASE_YEAR); > > mutex_lock(&rtc->lock); > ret = regmap_bulk_write(rtc->regmap, rtc->addr_base + RTC_TC_SEC, > @@ -165,8 +167,8 @@ static int mtk_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) > tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK; > tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK; > tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK; > - tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK; > - > + /* Apply the same offset conversion for alarm read */ > + tm->tm_year = (data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK) + (RTC_MIN_YEAR - RTC_BASE_YEAR); > tm->tm_mon--; > > return 0; > @@ -200,8 +202,9 @@ static int mtk_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) > (tm->tm_mday & RTC_AL_DOM_MASK)); > data[RTC_OFFSET_MTH] = ((data[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK)) | > (tm->tm_mon & RTC_AL_MTH_MASK)); > + /* Convert alarm year using the same offset as in read/write time */ > data[RTC_OFFSET_YEAR] = ((data[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK)) | > - (tm->tm_year & RTC_AL_YEA_MASK)); > + ((tm->tm_year - (RTC_MIN_YEAR - RTC_BASE_YEAR)) & RTC_AL_YEA_MASK)); > > if (alm->enabled) { > ret = regmap_bulk_write(rtc->regmap, > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] rtc: mt6397: Fix mt6357 RTC year offset handling for hwclock commands 2025-04-02 13:03 ` AngeloGioacchino Del Regno @ 2025-04-11 14:20 ` Alexandre Mergnat 0 siblings, 0 replies; 5+ messages in thread From: Alexandre Mergnat @ 2025-04-11 14:20 UTC (permalink / raw) To: AngeloGioacchino Del Regno, Eddie Huang, Sean Wang, Alexandre Belloni, Matthias Brugger, Macpaul Lin Cc: linux-arm-kernel, linux-mediatek, linux-rtc, linux-kernel On 02/04/2025 15:03, AngeloGioacchino Del Regno wrote: > Il 02/04/25 12:51, Alexandre Mergnat ha scritto: >> The mt6357 RTC was failing when using the `hwclock -r --verbose` command, >> despite reading correctly through sysfs. There is high chance for other >> platform to be impacted by the year offset handling issue. >> >> The hardware RTC registers store years relative to 1968, but the driver >> wasn't consistently applying the offset when converting between >> hardware and Linux time representation. >> >> This inconsistency caused alarm rollover failures during device >> registration, with the error "alarm rollover not handled -22" in the >> logs, causing hwclock commands to fail. >> >> The ioctl interface used by the hwclock command requires proper time >> range validation that wasn't happening with the inconsistent year >> offsets. >> >> Fixes the issue by applying the year offset in all operations: >> - Adding (RTC_MIN_YEAR - RTC_BASE_YEAR) when reading from hardware >> - Subtracting the same offset when writing to hardware >> - Using the same logic for both regular time and alarm operations >> >> With these changes, the hwclock command works correctly and time >> values are consistently handled across all interfaces. >> >> Signed-off-by: Alexandre Mergnat <amergnat@baylibre.com> >> --- >> drivers/rtc/rtc-mt6397.c | 13 ++++++++----- >> 1 file changed, 8 insertions(+), 5 deletions(-) >> >> diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c >> index 692c00ff544b2..ba52e225dc8fa 100644 >> --- a/drivers/rtc/rtc-mt6397.c >> +++ b/drivers/rtc/rtc-mt6397.c >> @@ -77,7 +77,8 @@ static int __mtk_rtc_read_time(struct mt6397_rtc *rtc, >> tm->tm_mday = data[RTC_OFFSET_DOM]; >> tm->tm_wday = data[RTC_OFFSET_DOW]; >> tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_TC_MTH_MASK; >> - tm->tm_year = data[RTC_OFFSET_YEAR]; >> + /* The RTC registers store years since 1968 (hardware's base year) */ >> + tm->tm_year = data[RTC_OFFSET_YEAR] + (RTC_MIN_YEAR - RTC_BASE_YEAR); > > This patch received a NACK because of RTC_MIN_YEAR_OFFSET. > > What you're doing here is avoiding to use the "RTC_MIN_YEAR_OFFSET" definition name > but otherwise doing the very same thing that was NACKed before. You're right sorry. In my mind, the rtc framework was working well, then I try-hard to fix the issue in this driver... but I was wrong. :( The RTC framework have issues so fixes should be in the framework directly. My next suggestion: https://lore.kernel.org/r/20250109-enable-rtc-v3-0-f003e8144419@baylibre.com -- Regards, Alexandre ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-04-11 14:20 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2025-04-02 10:50 [PATCH v2 0/2] Enable RTC for the MT6357 Alexandre Mergnat 2025-04-02 10:50 ` [PATCH v2 1/2] rtc: mt6359: add mt6357 support Alexandre Mergnat 2025-04-02 10:51 ` [PATCH v2 2/2] rtc: mt6397: Fix mt6357 RTC year offset handling for hwclock commands Alexandre Mergnat 2025-04-02 13:03 ` AngeloGioacchino Del Regno 2025-04-11 14:20 ` Alexandre Mergnat
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®