rtc: mediatek: add support for SoC based RTC on MT7622
[email protected]
maintainerv2 cover rtc: mediatek: add support for SoC based RTC on MT7622
From: Sean Wang <sean.wang-NuS5LvNUpcJWk0Htik3J/[email protected]>
Changes since v1:
- Rename to rtc-mt7622.c from rtc-mediatek.c
- Use [readl,writel]_relaxed instead of __raw_* version
- Remove redundant register reading in mtk_rtc_get_alarm_or_time()
- Stop alarm when alarm is really expired in mtk_rtc_alarmirq()
- Extend the maximum year the RTC/Alarm can count/support using year rebasing
- Remove unnesscary log which is already done in core layer
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-mt7622.c | 418 +++++++++++++++++++++ 5 files changed, 457 insertions(+), 4 deletions(-) create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mediatek.txt create mode 100644 drivers/rtc/rtc-mt7622.c
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
[email protected]
maintainerPATCH v2 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
@@ -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
Re: [PATCH v2 1/4] dt-bindings: rtc: mediatek: add bindings for MediaTek SoC based RTC
Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
Quoted 17 lines · On Tue, 2017-10-17 at 17:40 +0800, sean.wang-NuS5LvNUpcJWk0Htik3J/[email protected]…
From: Sean Wang <sean.wang-NuS5LvNUpcJWk0Htik3J/[email protected]>
Add device-tree binding for MediaTek SoC based RTC
Acked-by Rob Herring <[email protected]>--- .../devicetree/bindings/rtc/rtc-mediatek.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mediatek.txtDocumentation/devicetree/bindings/rtc/rtc-mediatek.txt new file +0
-0
How about change this filename to match driver filename change?
Joe.C
--
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
Sean Wang
maintainerRe: [PATCH v2 1/4] dt-bindings: rtc: mediatek: add bindings for MediaTek SoC based RTC
Quoted 22 lines · Yingjoe Chen
Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
Quoted 17 lines · On Tue, 2017-10-17 at 17:40 +0800, [email protected] wrote:
From: Sean Wang <[email protected]>
Add device-tree binding for MediaTek SoC based RTC
Signed-off-by Sean Wang <[email protected]>Acked-by Rob Herring <[email protected]>--- .../devicetree/bindings/rtc/rtc-mediatek.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mediatek.txtDocumentation/devicetree/bindings/rtc/rtc-mediatek.txt new file +0
-0How about change this filename to match driver filename change?
Quoted 2 lines
Joe.C
Will be improved together with the other patches
[email protected]
maintainerPATCH v2 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-mt7622.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 drivers/rtc/rtc-mt7622.c
drivers/rtc/Kconfig +10 -0
@@ -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
@@ -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-mt7622.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-mt7622.c new file +418 -0
418 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
Quoted 189 lines · On Tue, 2017-10-17 at 17:40 +0800, [email protected] wrote:
From: Sean Wang <[email protected]>
This patch introduces the driver for the RTC on MT7622 SoC.
Signed-off-by Sean Wang <[email protected]>--- drivers/rtc/Kconfig | 10 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-mt7622.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 drivers/rtc/rtc-mt7622.cdrivers/rtc/Kconfig +10
-0@@ -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@@ -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-mt7622.o obj-$(CONFIG_RTC_DRV_MT6397) += rtc-mt6397.o obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o obj-$(CONFIG_RTC_DRV_MXC) += rtc-mxc.odrivers/rtc/rtc-mt7622.c new file +138
-0@@ -0,0 +1,418 @@ +/* +* Driver for MediaTek SoC based RTC +* +* Copyright (C) 2017 Sean Wang <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/clk.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/of_address.h> +#include <linux/of_device.h> +#include <linux/platform_device.h> +#include <linux/rtc.h> + +#define MTK_RTC_DEV KBUILD_MODNAME + +#define MTK_RTC_PWRCHK1 0x4 +#define RTC_PWRCHK1_MAGIC 0xc6 + +#define MTK_RTC_PWRCHK2 0x8 +#define RTC_PWRCHK2_MAGIC 0x9a + +#define MTK_RTC_KEY 0xc +#define RTC_KEY_MAGIC 0x59 + +#define MTK_RTC_PROT1 0x10 +#define RTC_PROT1_MAGIC 0xa3 + +#define MTK_RTC_PROT2 0x14 +#define RTC_PROT2_MAGIC 0x57 + +#define MTK_RTC_PROT3 0x18 +#define RTC_PROT3_MAGIC 0x67 + +#define MTK_RTC_PROT4 0x1c +#define RTC_PROT4_MAGIC 0xd2 + +#define MTK_RTC_CTL 0x20 +#define RTC_RC_STOP BIT(0) + +#define MTK_RTC_DEBNCE 0x2c +#define RTC_DEBNCE_MASK GENMASK(2, 0) + +#define MTK_RTC_INT 0x30 +#define RTC_INT_AL_STA BIT(4) + +/* Ranges from 0x40 to 0x78 provide RTC time setup for year, month, +* day of month, day of week, hour, minute and second. +*/ +#define MTK_RTC_TREG (_t, _f) (0x40 + (0x4 * (_f)) + ((_t) * 0x20)) + +#define MTK_RTC_AL_CTL 0x7c +#define RTC_AL_EN BIT(0) +#define RTC_AL_ALL GENMASK(7, 0) + +#define MTK_RTC_TM_YR_L 100 + +/* The maximum years the RTC can support is 99, For MT7622 */ +#define MTK_RTC_HW_YR_LIMIT 100 + +/* Types of the function the RTC provides are time counter and alarm. */ +enum { +MTK_TC , +MTK_AL , +MTK_TYPE_MAX , +}; + +/* Indexes are used for the pointer to relevant registers in MTK_RTC_TREG */ +enum { +MTK_YEA , +MTK_MON , +MTK_DOM , +MTK_DOW , +MTK_HOU , +MTK_MIN , +MTK_SEC +}; + +struct mtk_rtc { +struct rtc_device *rtc; +void __iomem *base; +int irq; +struct clk *clk; +u32 yr_base[MTK_TYPE_MAX ]; +}; + +static void mtk_w32 (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +writel_relaxed (val, rtc->base + reg); +} + +static u32 mtk_r32 (struct mtk_rtc *rtc,u32 reg) +{ +return readl_relaxed (rtc->base + reg); +} + +static void mtk_rmw (struct mtk_rtc *rtc,u32 reg,u32 mask,u32 set) +{ +u32 val; + + val =mtk_r32 (rtc, reg); + val &= ~mask; + val |= set; +mtk_w32 (rtc, reg, val); +} + +static void mtk_set (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +mtk_rmw (rtc, reg,0 , val); +} + +static void mtk_clr (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +mtk_rmw (rtc, reg, val,0 ); +} + +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 );
drivers/rtc/rtc-mt7622.c line 148
Quoted 10 lines
drivers/rtc/rtc-mt7622.c new file +10
-0+} + +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 the field of the second is not changed which +* ensures all fields in the consistent state.
Sean,
You need to read sec first for this to work.
It is possible sec changed from 59 to 00 after you read min.
Joe.C
Quoted 270 lines
drivers/rtc/rtc-mt7622.c new file +270
-0+*/ +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 (sec !=mtk_r32 (hw,MTK_RTC_TREG (time_alarm,MTK_SEC ))); + + tm->tm_sec = sec; + tm->tm_min = min; + tm->tm_hour = hour; + tm->tm_wday = wday; + tm->tm_mday = mday; + tm->tm_mon = mon -1 ; + +/* Rebase to the absolute year which userspace requires */ + tm->tm_year = year +MTK_RTC_TM_YR_L + hw->yr_base[time_alarm]; +} + +static void mtk_rtc_set_alarm_or_time (struct mtk_rtc *hw,struct rtc_time *tm, +int time_alarm) +{ +u32 year; + +/* Rebase to the relative year which RTC hardware requires */ + year = tm->tm_year -MTK_RTC_TM_YR_L - hw->yr_base[time_alarm]; + +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_YEA ), year); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_MON ), tm->tm_mon +1 ); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_DOW ), tm->tm_wday); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_DOM ), tm->tm_mday); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_HOU ), tm->tm_hour); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_MIN ), tm->tm_min); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_SEC ), tm->tm_sec); +} + +static irqreturn_t mtk_rtc_alarmirq (int irq,void *id) +{ +struct mtk_rtc *hw = (struct mtk_rtc *)id; +u32 irq_sta; + + irq_sta =mtk_r32 (hw,MTK_RTC_INT ); +if (irq_sta &RTC_INT_AL_STA ) { +/* Stop alarm also implicitly disables the alarm interrupt */ +mtk_w32 (hw,MTK_RTC_AL_CTL ,0 ); +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); +} + +static int mtk_rtc_settime (struct device *dev,struct rtc_time *tm) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); + +/* Epoch == 1900, and the driver assumes that tm->tm_year has to be +* at least MTK_RTC_TM_YR_L. +*/ +if (tm->tm_year <MTK_RTC_TM_YR_L ) +return -EINVAL ; + +/* Keep yr_base used to calculate the calculate year when userspace +* queries and extend the maximum year the RTC can count. +*/ + hw->yr_base[MTK_TC ] = tm->tm_year -MTK_RTC_TM_YR_L - + (tm->tm_year %MTK_RTC_HW_YR_LIMIT ); + +/* Stop time counter before setting a new one*/ +mtk_set (hw,MTK_RTC_CTL ,RTC_RC_STOP ); + +mtk_rtc_set_alarm_or_time (hw, tm,MTK_TC ); + +/* Restart the time counter */ +mtk_clr (hw,MTK_RTC_CTL ,RTC_RC_STOP ); + +return 0 ; +} + +static int mtk_rtc_getalarm (struct device *dev,struct rtc_wkalrm *wkalrm) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); +struct rtc_time *alrm_tm = &wkalrm->time; + +mtk_rtc_get_alarm_or_time (hw, alrm_tm,MTK_AL ); + + wkalrm->enabled = !!(mtk_r32 (hw,MTK_RTC_AL_CTL ) &RTC_AL_EN ); + wkalrm->pending = !!(mtk_r32 (hw,MTK_RTC_INT ) &RTC_INT_AL_STA ); + +return 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, and the driver assumes that alrm_tm->tm_year has to +* be at least MTK_RTC_TM_YR_L. +*/ +if (alrm_tm->tm_year <MTK_RTC_TM_YR_L ) +return -EINVAL ; + +/* Keep yr_base used to calculate the calculate year when userspace +* queries and extend the maximum time the alarm can support. +*/ + hw->yr_base[MTK_AL ] = alrm_tm->tm_year -MTK_RTC_TM_YR_L - + (alrm_tm->tm_year %MTK_RTC_HW_YR_LIMIT ); + +/* +* Stop the alarm also implicitly including disables interrupt before +* setting a new one. +*/ +mtk_clr (hw,MTK_RTC_AL_CTL ,RTC_AL_EN ); + +/* +* Avoid contention between mtk_rtc_setalarm and IRQ handler so that +* disabling the interrupt and awaiting for pending IRQ handler to +* complete. +*/ +synchronize_irq (hw->irq); + +mtk_rtc_set_alarm_or_time (hw, alrm_tm,MTK_AL ); + +/* Restart the alarm with the new setup */ +mtk_w32 (hw,MTK_RTC_AL_CTL ,RTC_AL_ALL ); + +return 0 ; +} + +static const struct rtc_class_ops mtk_rtc_ops = { + .read_time = mtk_rtc_gettime, + .set_time = mtk_rtc_settime, + .read_alarm = mtk_rtc_getalarm, + .set_alarm = mtk_rtc_setalarm, +}; + +static const struct of_device_id mtk_rtc_match[] = { + { .compatible ="mediatek,mt7622-rtc" }, + { .compatible ="mediatek,soc-rtc" }, + {}, +}; + +static int mtk_rtc_probe (struct platform_device *pdev) +{ +struct mtk_rtc *hw; +struct resource *res; +int ret; + + hw =devm_kzalloc (&pdev->dev,sizeof (*hw),GFP_KERNEL ); +if (!hw) +return -ENOMEM ; + +platform_set_drvdata (pdev, hw); + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); + hw->base =devm_ioremap_resource (&pdev->dev, res); +if (IS_ERR (hw->base)) +return PTR_ERR (hw->base); + + hw->clk =devm_clk_get (&pdev->dev,"rtc" ); +if (IS_ERR (hw->clk)) { +dev_err (&pdev->dev,"No clock\n" ); +return PTR_ERR (hw->clk); + } + + ret =clk_prepare_enable (hw->clk); +if (ret) +return ret; + + hw->irq =platform_get_irq (pdev,0 ); +if (hw->irq <0 ) { +dev_err (&pdev->dev,"No IRQ resource\n" ); + ret = hw->irq; + goto err; + } + + ret =devm_request_irq (&pdev->dev, hw->irq, mtk_rtc_alarmirq, +0 ,dev_name (&pdev->dev), hw); +if (ret) { +dev_err (&pdev->dev,"Can't request IRQ\n" ); + goto err; + } + +mtk_rtc_hw_init (hw); + +device_init_wakeup (&pdev->dev, true); + + hw->rtc =devm_rtc_device_register (&pdev->dev, pdev->name, + &mtk_rtc_ops,THIS_MODULE ); +if (IS_ERR (hw->rtc)) { + ret =PTR_ERR (hw->rtc); +dev_err (&pdev->dev,"Unable to register device\n" ); + goto err; + } + +return 0 ; +err: +clk_disable_unprepare (hw->clk); + +return ret; +} + +static int mtk_rtc_remove (struct platform_device *pdev) +{ +struct mtk_rtc *hw =platform_get_drvdata (pdev); + +clk_disable_unprepare (hw->clk); + +return 0 ; +} + +#ifdef CONFIG_PM_SLEEP +static int mtk_rtc_suspend (struct device *dev) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); + +if (device_may_wakeup (dev)) +enable_irq_wake (hw->irq); + +return 0 ; +} + +static int mtk_rtc_resume (struct device *dev) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); + +if (device_may_wakeup (dev)) +disable_irq_wake (hw->irq); + +return 0 ; +} + +static SIMPLE_DEV_PM_OPS(mtk_rtc_pm_ops, mtk_rtc_suspend, mtk_rtc_resume); + +#define MTK_RTC_PM_OPS (&mtk_rtc_pm_ops) +#else /* CONFIG_PM */ +#define MTK_RTC_PM_OPS NULL +#endif /* CONFIG_PM */ + +static struct platform_driver mtk_rtc_driver = { + .probe = mtk_rtc_probe, + .remove = mtk_rtc_remove, + .driver = { + .name =MTK_RTC_DEV , + .of_match_table = mtk_rtc_match, + .pm =MTK_RTC_PM_OPS , + }, +}; + +module_platform_driver (mtk_rtc_driver); + +MODULE_DESCRIPTION ("MediaTek SoC based RTC Driver" ); +MODULE_AUTHOR ("Sean Wang <[email protected]>" ); +MODULE_LICENSE ("GPL" );
Sean Wang
maintainerRe: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
Quoted 203 lines · Yingjoe Chen
Quoted 189 lines · On Tue, 2017-10-17 at 17:40 +0800, [email protected] wrote:
From: Sean Wang <[email protected]>
This patch introduces the driver for the RTC on MT7622 SoC.
Signed-off-by Sean Wang <[email protected]>--- drivers/rtc/Kconfig | 10 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-mt7622.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 drivers/rtc/rtc-mt7622.cdrivers/rtc/Kconfig +10
-0@@ -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@@ -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-mt7622.o obj-$(CONFIG_RTC_DRV_MT6397) += rtc-mt6397.o obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o obj-$(CONFIG_RTC_DRV_MXC) += rtc-mxc.odrivers/rtc/rtc-mt7622.c new file +138
-0@@ -0,0 +1,418 @@ +/* +* Driver for MediaTek SoC based RTC +* +* Copyright (C) 2017 Sean Wang <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/clk.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/of_address.h> +#include <linux/of_device.h> +#include <linux/platform_device.h> +#include <linux/rtc.h> + +#define MTK_RTC_DEV KBUILD_MODNAME + +#define MTK_RTC_PWRCHK1 0x4 +#define RTC_PWRCHK1_MAGIC 0xc6 + +#define MTK_RTC_PWRCHK2 0x8 +#define RTC_PWRCHK2_MAGIC 0x9a + +#define MTK_RTC_KEY 0xc +#define RTC_KEY_MAGIC 0x59 + +#define MTK_RTC_PROT1 0x10 +#define RTC_PROT1_MAGIC 0xa3 + +#define MTK_RTC_PROT2 0x14 +#define RTC_PROT2_MAGIC 0x57 + +#define MTK_RTC_PROT3 0x18 +#define RTC_PROT3_MAGIC 0x67 + +#define MTK_RTC_PROT4 0x1c +#define RTC_PROT4_MAGIC 0xd2 + +#define MTK_RTC_CTL 0x20 +#define RTC_RC_STOP BIT(0) + +#define MTK_RTC_DEBNCE 0x2c +#define RTC_DEBNCE_MASK GENMASK(2, 0) + +#define MTK_RTC_INT 0x30 +#define RTC_INT_AL_STA BIT(4) + +/* Ranges from 0x40 to 0x78 provide RTC time setup for year, month, +* day of month, day of week, hour, minute and second. +*/ +#define MTK_RTC_TREG (_t, _f) (0x40 + (0x4 * (_f)) + ((_t) * 0x20)) + +#define MTK_RTC_AL_CTL 0x7c +#define RTC_AL_EN BIT(0) +#define RTC_AL_ALL GENMASK(7, 0) + +#define MTK_RTC_TM_YR_L 100 + +/* The maximum years the RTC can support is 99, For MT7622 */ +#define MTK_RTC_HW_YR_LIMIT 100 + +/* Types of the function the RTC provides are time counter and alarm. */ +enum { +MTK_TC , +MTK_AL , +MTK_TYPE_MAX , +}; + +/* Indexes are used for the pointer to relevant registers in MTK_RTC_TREG */ +enum { +MTK_YEA , +MTK_MON , +MTK_DOM , +MTK_DOW , +MTK_HOU , +MTK_MIN , +MTK_SEC +}; + +struct mtk_rtc { +struct rtc_device *rtc; +void __iomem *base; +int irq; +struct clk *clk; +u32 yr_base[MTK_TYPE_MAX ]; +}; + +static void mtk_w32 (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +writel_relaxed (val, rtc->base + reg); +} + +static u32 mtk_r32 (struct mtk_rtc *rtc,u32 reg) +{ +return readl_relaxed (rtc->base + reg); +} + +static void mtk_rmw (struct mtk_rtc *rtc,u32 reg,u32 mask,u32 set) +{ +u32 val; + + val =mtk_r32 (rtc, reg); + val &= ~mask; + val |= set; +mtk_w32 (rtc, reg, val); +} + +static void mtk_set (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +mtk_rmw (rtc, reg,0 , val); +} + +static void mtk_clr (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +mtk_rmw (rtc, reg, val,0 ); +} + +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 );drivers/rtc/rtc-mt7622.c line 148
Quoted 10 lines
drivers/rtc/rtc-mt7622.c new file +10
-0+} + +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 the field of the second is not changed which +* ensures all fields in the consistent state. Sean,
Quoted 5 lines
You need to read sec first for this to work.
It is possible sec changed from 59 to 00 after you read min.Joe.C
Nice catch. It will be fixed. Thanks for your careful review.
Sean
Quoted 272 lines
Quoted 270 lines
drivers/rtc/rtc-mt7622.c new file +270
-0+*/ +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 (sec !=mtk_r32 (hw,MTK_RTC_TREG (time_alarm,MTK_SEC ))); + + tm->tm_sec = sec; + tm->tm_min = min; + tm->tm_hour = hour; + tm->tm_wday = wday; + tm->tm_mday = mday; + tm->tm_mon = mon -1 ; + +/* Rebase to the absolute year which userspace requires */ + tm->tm_year = year +MTK_RTC_TM_YR_L + hw->yr_base[time_alarm]; +} + +static void mtk_rtc_set_alarm_or_time (struct mtk_rtc *hw,struct rtc_time *tm, +int time_alarm) +{ +u32 year; + +/* Rebase to the relative year which RTC hardware requires */ + year = tm->tm_year -MTK_RTC_TM_YR_L - hw->yr_base[time_alarm]; + +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_YEA ), year); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_MON ), tm->tm_mon +1 ); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_DOW ), tm->tm_wday); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_DOM ), tm->tm_mday); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_HOU ), tm->tm_hour); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_MIN ), tm->tm_min); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_SEC ), tm->tm_sec); +} + +static irqreturn_t mtk_rtc_alarmirq (int irq,void *id) +{ +struct mtk_rtc *hw = (struct mtk_rtc *)id; +u32 irq_sta; + + irq_sta =mtk_r32 (hw,MTK_RTC_INT ); +if (irq_sta &RTC_INT_AL_STA ) { +/* Stop alarm also implicitly disables the alarm interrupt */ +mtk_w32 (hw,MTK_RTC_AL_CTL ,0 ); +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); +} + +static int mtk_rtc_settime (struct device *dev,struct rtc_time *tm) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); + +/* Epoch == 1900, and the driver assumes that tm->tm_year has to be +* at least MTK_RTC_TM_YR_L. +*/ +if (tm->tm_year <MTK_RTC_TM_YR_L ) +return -EINVAL ; + +/* Keep yr_base used to calculate the calculate year when userspace +* queries and extend the maximum year the RTC can count. +*/ + hw->yr_base[MTK_TC ] = tm->tm_year -MTK_RTC_TM_YR_L - + (tm->tm_year %MTK_RTC_HW_YR_LIMIT ); + +/* Stop time counter before setting a new one*/ +mtk_set (hw,MTK_RTC_CTL ,RTC_RC_STOP ); + +mtk_rtc_set_alarm_or_time (hw, tm,MTK_TC ); + +/* Restart the time counter */ +mtk_clr (hw,MTK_RTC_CTL ,RTC_RC_STOP ); + +return 0 ; +} + +static int mtk_rtc_getalarm (struct device *dev,struct rtc_wkalrm *wkalrm) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); +struct rtc_time *alrm_tm = &wkalrm->time; + +mtk_rtc_get_alarm_or_time (hw, alrm_tm,MTK_AL ); + + wkalrm->enabled = !!(mtk_r32 (hw,MTK_RTC_AL_CTL ) &RTC_AL_EN ); + wkalrm->pending = !!(mtk_r32 (hw,MTK_RTC_INT ) &RTC_INT_AL_STA ); + +return 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, and the driver assumes that alrm_tm->tm_year has to +* be at least MTK_RTC_TM_YR_L. +*/ +if (alrm_tm->tm_year <MTK_RTC_TM_YR_L ) +return -EINVAL ; + +/* Keep yr_base used to calculate the calculate year when userspace +* queries and extend the maximum time the alarm can support. +*/ + hw->yr_base[MTK_AL ] = alrm_tm->tm_year -MTK_RTC_TM_YR_L - + (alrm_tm->tm_year %MTK_RTC_HW_YR_LIMIT ); + +/* +* Stop the alarm also implicitly including disables interrupt before +* setting a new one. +*/ +mtk_clr (hw,MTK_RTC_AL_CTL ,RTC_AL_EN ); + +/* +* Avoid contention between mtk_rtc_setalarm and IRQ handler so that +* disabling the interrupt and awaiting for pending IRQ handler to +* complete. +*/ +synchronize_irq (hw->irq); + +mtk_rtc_set_alarm_or_time (hw, alrm_tm,MTK_AL ); + +/* Restart the alarm with the new setup */ +mtk_w32 (hw,MTK_RTC_AL_CTL ,RTC_AL_ALL ); + +return 0 ; +} + +static const struct rtc_class_ops mtk_rtc_ops = { + .read_time = mtk_rtc_gettime, + .set_time = mtk_rtc_settime, + .read_alarm = mtk_rtc_getalarm, + .set_alarm = mtk_rtc_setalarm, +}; + +static const struct of_device_id mtk_rtc_match[] = { + { .compatible ="mediatek,mt7622-rtc" }, + { .compatible ="mediatek,soc-rtc" }, + {}, +}; + +static int mtk_rtc_probe (struct platform_device *pdev) +{ +struct mtk_rtc *hw; +struct resource *res; +int ret; + + hw =devm_kzalloc (&pdev->dev,sizeof (*hw),GFP_KERNEL ); +if (!hw) +return -ENOMEM ; + +platform_set_drvdata (pdev, hw); + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); + hw->base =devm_ioremap_resource (&pdev->dev, res); +if (IS_ERR (hw->base)) +return PTR_ERR (hw->base); + + hw->clk =devm_clk_get (&pdev->dev,"rtc" ); +if (IS_ERR (hw->clk)) { +dev_err (&pdev->dev,"No clock\n" ); +return PTR_ERR (hw->clk); + } + + ret =clk_prepare_enable (hw->clk); +if (ret) +return ret; + + hw->irq =platform_get_irq (pdev,0 ); +if (hw->irq <0 ) { +dev_err (&pdev->dev,"No IRQ resource\n" ); + ret = hw->irq; + goto err; + } + + ret =devm_request_irq (&pdev->dev, hw->irq, mtk_rtc_alarmirq, +0 ,dev_name (&pdev->dev), hw); +if (ret) { +dev_err (&pdev->dev,"Can't request IRQ\n" ); + goto err; + } + +mtk_rtc_hw_init (hw); + +device_init_wakeup (&pdev->dev, true); + + hw->rtc =devm_rtc_device_register (&pdev->dev, pdev->name, + &mtk_rtc_ops,THIS_MODULE ); +if (IS_ERR (hw->rtc)) { + ret =PTR_ERR (hw->rtc); +dev_err (&pdev->dev,"Unable to register device\n" ); + goto err; + } + +return 0 ; +err: +clk_disable_unprepare (hw->clk); + +return ret; +} + +static int mtk_rtc_remove (struct platform_device *pdev) +{ +struct mtk_rtc *hw =platform_get_drvdata (pdev); + +clk_disable_unprepare (hw->clk); + +return 0 ; +} + +#ifdef CONFIG_PM_SLEEP +static int mtk_rtc_suspend (struct device *dev) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); + +if (device_may_wakeup (dev)) +enable_irq_wake (hw->irq); + +return 0 ; +} + +static int mtk_rtc_resume (struct device *dev) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); + +if (device_may_wakeup (dev)) +disable_irq_wake (hw->irq); + +return 0 ; +} + +static SIMPLE_DEV_PM_OPS(mtk_rtc_pm_ops, mtk_rtc_suspend, mtk_rtc_resume); + +#define MTK_RTC_PM_OPS (&mtk_rtc_pm_ops) +#else /* CONFIG_PM */ +#define MTK_RTC_PM_OPS NULL +#endif /* CONFIG_PM */ + +static struct platform_driver mtk_rtc_driver = { + .probe = mtk_rtc_probe, + .remove = mtk_rtc_remove, + .driver = { + .name =MTK_RTC_DEV , + .of_match_table = mtk_rtc_match, + .pm =MTK_RTC_PM_OPS , + }, +}; + +module_platform_driver (mtk_rtc_driver); + +MODULE_DESCRIPTION ("MediaTek SoC based RTC Driver" ); +MODULE_AUTHOR ("Sean Wang <[email protected]>" ); +MODULE_LICENSE ("GPL" );
Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
In reply to [email protected]Quoted 17 lines · On Tue, 2017-10-17 at 17:40 +0800, sean.wang-NuS5LvNUpcJWk0Htik3J/[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-mt7622.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 drivers/rtc/rtc-mt7622.cdrivers/rtc/Kconfig +0
-0
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1708
Quoted 4 lines
drivers/rtc/Kconfig +1
-0@@ -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
How about changing this to RTC_DRV_MT7622 or RTC_DRV_MEDIATEK_SOC? It is confusing to have both RTC_DRV_MEDIATEK & RTC_DRV_MT6397 here.
Quoted 243 lines
drivers/rtc/Kconfig +9
-0+ 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@@ -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-mt7622.o obj-$(CONFIG_RTC_DRV_MT6397) += rtc-mt6397.o obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o obj-$(CONFIG_RTC_DRV_MXC) += rtc-mxc.odrivers/rtc/rtc-mt7622.c new file +213
-0@@ -0,0 +1,418 @@ +/* +* Driver for MediaTek SoC based RTC +* +* Copyright (C) 2017 Sean Wang <sean.wang-NuS5LvNUpcJWk0Htik3J/[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/clk.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/of_address.h> +#include <linux/of_device.h> +#include <linux/platform_device.h> +#include <linux/rtc.h> + +#define MTK_RTC_DEV KBUILD_MODNAME + +#define MTK_RTC_PWRCHK1 0x4 +#define RTC_PWRCHK1_MAGIC 0xc6 + +#define MTK_RTC_PWRCHK2 0x8 +#define RTC_PWRCHK2_MAGIC 0x9a + +#define MTK_RTC_KEY 0xc +#define RTC_KEY_MAGIC 0x59 + +#define MTK_RTC_PROT1 0x10 +#define RTC_PROT1_MAGIC 0xa3 + +#define MTK_RTC_PROT2 0x14 +#define RTC_PROT2_MAGIC 0x57 + +#define MTK_RTC_PROT3 0x18 +#define RTC_PROT3_MAGIC 0x67 + +#define MTK_RTC_PROT4 0x1c +#define RTC_PROT4_MAGIC 0xd2 + +#define MTK_RTC_CTL 0x20 +#define RTC_RC_STOP BIT(0) + +#define MTK_RTC_DEBNCE 0x2c +#define RTC_DEBNCE_MASK GENMASK(2, 0) + +#define MTK_RTC_INT 0x30 +#define RTC_INT_AL_STA BIT(4) + +/* Ranges from 0x40 to 0x78 provide RTC time setup for year, month, +* day of month, day of week, hour, minute and second. +*/ +#define MTK_RTC_TREG (_t, _f) (0x40 + (0x4 * (_f)) + ((_t) * 0x20)) + +#define MTK_RTC_AL_CTL 0x7c +#define RTC_AL_EN BIT(0) +#define RTC_AL_ALL GENMASK(7, 0) + +#define MTK_RTC_TM_YR_L 100 + +/* The maximum years the RTC can support is 99, For MT7622 */ +#define MTK_RTC_HW_YR_LIMIT 100 + +/* Types of the function the RTC provides are time counter and alarm. */ +enum { +MTK_TC , +MTK_AL , +MTK_TYPE_MAX , +}; + +/* Indexes are used for the pointer to relevant registers in MTK_RTC_TREG */ +enum { +MTK_YEA , +MTK_MON , +MTK_DOM , +MTK_DOW , +MTK_HOU , +MTK_MIN , +MTK_SEC +}; + +struct mtk_rtc { +struct rtc_device *rtc; +void __iomem *base; +int irq; +struct clk *clk; +u32 yr_base[MTK_TYPE_MAX ]; +}; + +static void mtk_w32 (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +writel_relaxed (val, rtc->base + reg); +} + +static u32 mtk_r32 (struct mtk_rtc *rtc,u32 reg) +{ +return readl_relaxed (rtc->base + reg); +} + +static void mtk_rmw (struct mtk_rtc *rtc,u32 reg,u32 mask,u32 set) +{ +u32 val; + + val =mtk_r32 (rtc, reg); + val &= ~mask; + val |= set; +mtk_w32 (rtc, reg, val); +} + +static void mtk_set (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +mtk_rmw (rtc, reg,0 , val); +} + +static void mtk_clr (struct mtk_rtc *rtc,u32 reg,u32 val) +{ +mtk_rmw (rtc, reg, val,0 ); +} + +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 the field of the second is not changed which +* ensures 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 (sec !=mtk_r32 (hw,MTK_RTC_TREG (time_alarm,MTK_SEC ))); + + tm->tm_sec = sec; + tm->tm_min = min; + tm->tm_hour = hour; + tm->tm_wday = wday; + tm->tm_mday = mday; + tm->tm_mon = mon -1 ; + +/* Rebase to the absolute year which userspace requires */ + tm->tm_year = year +MTK_RTC_TM_YR_L + hw->yr_base[time_alarm]; +} + +static void mtk_rtc_set_alarm_or_time (struct mtk_rtc *hw,struct rtc_time *tm, +int time_alarm) +{ +u32 year; + +/* Rebase to the relative year which RTC hardware requires */ + year = tm->tm_year -MTK_RTC_TM_YR_L - hw->yr_base[time_alarm]; + +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_YEA ), year); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_MON ), tm->tm_mon +1 ); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_DOW ), tm->tm_wday); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_DOM ), tm->tm_mday); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_HOU ), tm->tm_hour); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_MIN ), tm->tm_min); +mtk_w32 (hw,MTK_RTC_TREG (time_alarm,MTK_SEC ), tm->tm_sec); +} + +static irqreturn_t mtk_rtc_alarmirq (int irq,void *id) +{ +struct mtk_rtc *hw = (struct mtk_rtc *)id; +u32 irq_sta; + + irq_sta =mtk_r32 (hw,MTK_RTC_INT ); +if (irq_sta &RTC_INT_AL_STA ) { +/* Stop alarm also implicitly disables the alarm interrupt */ +mtk_w32 (hw,MTK_RTC_AL_CTL ,0 ); +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-mt7622.c line 223
Quoted 10 lines
drivers/rtc/rtc-mt7622.c new file +10
-0+} + +static int mtk_rtc_settime (struct device *dev,struct rtc_time *tm) +{ +struct mtk_rtc *hw =dev_get_drvdata (dev); + +/* Epoch == 1900, and the driver assumes that tm->tm_year has to be +* at least MTK_RTC_TM_YR_L. +*/ +if (tm->tm_year <MTK_RTC_TM_YR_L )
nit: extra space after <
drivers/rtc/rtc-mt7622.c line 230
Quoted 7 lines
drivers/rtc/rtc-mt7622.c new file +7
-0+return -EINVAL ; + +/* Keep yr_base used to calculate the calculate year when userspace +* queries and extend the maximum year the RTC can count. +*/ + hw->yr_base[MTK_TC ] = tm->tm_year -MTK_RTC_TM_YR_L - + (tm->tm_year %MTK_RTC_HW_YR_LIMIT );
I'm not sure this worth it.
If maximum year it can hold is 99, I'd bet it won't support leap year correctly after 2100. This make the RTC useless after that.
Also, yr_base is lost after power cycle, so you can't get correct year back anyway.
Joe.C
--
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
Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
Quoted 21 lines · Yingjoe Chen
Quoted 20 lines · On Tue, 2017-10-17 at 17:40 +0800, [email protected] wrote:
From: Sean Wang <[email protected]>
This patch introduces the driver for the RTC on MT7622 SoC.
Signed-off-by Sean Wang <[email protected]>--- drivers/rtc/Kconfig | 10 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-mt7622.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 drivers/rtc/rtc-mt7622.cdrivers/rtc/Kconfig +0
-0@@ -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
Quoted 5 lines
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1708
Quoted 1 line
drivers/rtc/Kconfig +1
-0+config RTC_DRV_MEDIATEKHow about changing this to
RTC_DRV_MT7622orRTC_DRV_MEDIATEK_SOC? It is confusing to have bothRTC_DRV_MEDIATEK&RTC_DRV_MT6397here.
Yes, this has to be RTC_DRV_MT7622. It doesn't matter if it support future SoCs named differently, it will be less confusing than using anything with only mediatek in it.
Quoted 13 lines
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1715
Quoted 7 lines
drivers/rtc/Kconfig +7
-0+ return -EINVAL; + + /* Keep yr_base used to calculate the calculate year when userspace + * queries and extend the maximum year the RTC can count. + */ + hw->yr_base[MTK_TC] = tm->tm_year - MTK_RTC_TM_YR_L - + (tm->tm_year % MTK_RTC_HW_YR_LIMIT);I'm not sure this worth it.
If maximum year it can hold is 99, I'd bet it won't support leap year correctly after 2100. This make the RTC useless after that.
Quoted 3 lines
Also,
yr_baseis lost after power cycle, so you can't get correct year back anyway.
I agree, the best you can do here is to only support 2000 to 2099.
Signature · 3 lines
Alexandre Belloni, Free Electrons Embedded Linux and Kernel engineering http://free-electrons.com
Sean Wang
maintainerRe: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
Hi, both
Quoted 28 lines · Alexandre Belloni
Quoted 21 lines · Yingjoe Chen
Quoted 20 lines · On Tue, 2017-10-17 at 17:40 +0800, sean.wang-NuS5LvNUpcJWk0Htik3J/[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-mt7622.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 drivers/rtc/rtc-mt7622.cdrivers/rtc/Kconfig +0
-0@@ -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-moxartQuoted 5 lines
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1708
Quoted 1 line
drivers/rtc/Kconfig +1
-0+config RTC_DRV_MEDIATEKHow about changing this to
RTC_DRV_MT7622orRTC_DRV_MEDIATEK_SOC? It is confusing to have bothRTC_DRV_MEDIATEK&RTC_DRV_MT6397here.
Quoted 4 lines
Yes, this has to be
RTC_DRV_MT7622. It doesn't matter if it support future SoCs named differently, it will be less confusing than using anything with only mediatek in it.
Agreed on. RTC_DRV_MT7622 will be applied instead to align the usage on MT6397 and to get rid of such kind of confusion.
Quoted 13 lines
Quoted 13 lines
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1715
Quoted 7 lines
drivers/rtc/Kconfig +7
-0+ return -EINVAL; + + /* Keep yr_base used to calculate the calculate year when userspace + * queries and extend the maximum year the RTC can count. + */ + hw->yr_base[MTK_TC] = tm->tm_year - MTK_RTC_TM_YR_L - + (tm->tm_year % MTK_RTC_HW_YR_LIMIT);I'm not sure this worth it.
If maximum year it can hold is 99, I'd bet it won't support leap year correctly after 2100. This make the RTC useless after that.
Quoted 6 lines
Quoted 3 lines
Also,
yr_baseis lost after power cycle, so you can't get correct year back anyway.I agree, the best you can do here is to only support 2000 to 2099.
O.K. I will remove those yr_base extension and only consider only support from 2000 to 2099 because of no much gain we can get from yr_base.
The only gain is yr_base I thought just allows people have the opportunity to set up rtc after 2100. However, it appears to not much practical to foresee these things after 2100 and rtc must be setup again when either year overflowing or power cycle happens after 2100 as Joe.C mentioned.
In addition, I also found the rtc hardware would take year == 0 as not leap year that works for 2100, 2200, 2300, but not for 2000, 2400, 2800,... and thus 2000 is also needed to be excluded in both set_time and set_alarm if only 2000 to 2099 is supported.
Sean
--
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
Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
Quoted 70 lines · Sean Wang
Hi, both
Quoted 28 lines · Alexandre Belloni
Quoted 21 lines · Yingjoe Chen
Quoted 20 lines · On Tue, 2017-10-17 at 17:40 +0800, [email protected] wrote:
From: Sean Wang <[email protected]>
This patch introduces the driver for the RTC on MT7622 SoC.
Signed-off-by Sean Wang <[email protected]>--- drivers/rtc/Kconfig | 10 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-mt7622.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 drivers/rtc/rtc-mt7622.cdrivers/rtc/Kconfig +0
-0@@ -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-moxartQuoted 5 lines
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1708
Quoted 1 line
drivers/rtc/Kconfig +1
-0+config RTC_DRV_MEDIATEKHow about changing this to
RTC_DRV_MT7622orRTC_DRV_MEDIATEK_SOC? It is confusing to have bothRTC_DRV_MEDIATEK&RTC_DRV_MT6397here.Quoted 4 lines
Yes, this has to be
RTC_DRV_MT7622. It doesn't matter if it support future SoCs named differently, it will be less confusing than using anything with only mediatek in it.Agreed on.
RTC_DRV_MT7622will be applied instead to align the usage on MT6397 and to get rid of such kind of confusion.Quoted 13 lines
Quoted 13 lines
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1715
Quoted 7 lines
drivers/rtc/Kconfig +7
-0+ return -EINVAL; + + /* Keep yr_base used to calculate the calculate year when userspace + * queries and extend the maximum year the RTC can count. + */ + hw->yr_base[MTK_TC] = tm->tm_year - MTK_RTC_TM_YR_L - + (tm->tm_year % MTK_RTC_HW_YR_LIMIT);I'm not sure this worth it.
If maximum year it can hold is 99, I'd bet it won't support leap year correctly after 2100. This make the RTC useless after that.Quoted 6 lines
Quoted 3 lines
Also,
yr_baseis lost after power cycle, so you can't get correct year back anyway.I agree, the best you can do here is to only support 2000 to 2099.
O.K. I will remove those
yr_baseextension and only consider only support from 2000 to 2099 because of no much gain we can get fromyr_base.The only gain is
yr_baseI thought just allows people have the opportunity to set up rtc after 2100. However, it appears to not much practical to foresee these things after 2100 and rtc must be setup again when either year overflowing or power cycle happens after 2100 as Joe.C mentioned.
Quoted 5 lines
In addition, I also found the rtc hardware would take year == 0 as not leap year that works for 2100, 2200, 2300, but not for 2000, 2400, 2800,... and thus 2000 is also needed to be excluded in both
set_timeandset_alarmif only 2000 to 2099 is supported.
So you can make it work from 2001 to 2100 but I'm not sure it is worth it.
Signature · 3 lines
Alexandre Belloni, Free Electrons Embedded Linux and Kernel engineering http://free-electrons.com
Sean Wang
maintainerRe: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
Quoted 71 lines · Alexandre Belloni
Quoted 70 lines · Sean Wang
Hi, both
Quoted 28 lines · Alexandre Belloni
Quoted 21 lines · Yingjoe Chen
Quoted 20 lines · On Tue, 2017-10-17 at 17:40 +0800, sean.wang-NuS5LvNUpcJWk0Htik3J/[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-mt7622.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 drivers/rtc/rtc-mt7622.cdrivers/rtc/Kconfig +0
-0@@ -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-moxartQuoted 5 lines
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1708
Quoted 1 line
drivers/rtc/Kconfig +1
-0+config RTC_DRV_MEDIATEKHow about changing this to
RTC_DRV_MT7622orRTC_DRV_MEDIATEK_SOC? It is confusing to have bothRTC_DRV_MEDIATEK&RTC_DRV_MT6397here.Quoted 4 lines
Yes, this has to be
RTC_DRV_MT7622. It doesn't matter if it support future SoCs named differently, it will be less confusing than using anything with only mediatek in it.Agreed on.
RTC_DRV_MT7622will be applied instead to align the usage on MT6397 and to get rid of such kind of confusion.Quoted 13 lines
Quoted 13 lines
drivers/rtc/Kconfig config RTC_DRV_MOXART · line 1715
Quoted 7 lines
drivers/rtc/Kconfig +7
-0+ return -EINVAL; + + /* Keep yr_base used to calculate the calculate year when userspace + * queries and extend the maximum year the RTC can count. + */ + hw->yr_base[MTK_TC] = tm->tm_year - MTK_RTC_TM_YR_L - + (tm->tm_year % MTK_RTC_HW_YR_LIMIT);I'm not sure this worth it.
If maximum year it can hold is 99, I'd bet it won't support leap year correctly after 2100. This make the RTC useless after that.Quoted 6 lines
Quoted 3 lines
Also,
yr_baseis lost after power cycle, so you can't get correct year back anyway.I agree, the best you can do here is to only support 2000 to 2099.
O.K. I will remove those
yr_baseextension and only consider only support from 2000 to 2099 because of no much gain we can get fromyr_base.The only gain is
yr_baseI thought just allows people have the opportunity to set up rtc after 2100. However, it appears to not much practical to foresee these things after 2100 and rtc must be setup again when either year overflowing or power cycle happens after 2100 as Joe.C mentioned.
Quoted 9 lines
Quoted 5 lines
In addition, I also found the rtc hardware would take year == 0 as not leap year that works for 2100, 2200, 2300, but not for 2000, 2400, 2800,... and thus 2000 is also needed to be excluded in both
set_timeandset_alarmif only 2000 to 2099 is supported.So you can make it work from 2001 to 2100 but I'm not sure it is worth it.
I prefer to 2001 to 2099 only which can be completely handled in the common logic. Extra specific logic simply for 2100 seems not be worthwhile.
--
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
Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
Quoted 4 lines · Sean Wang
I prefer to 2001 to 2099 only which can be completely handled in the common logic. Extra specific logic simply for 2100 seems not be worthwhile.
Agreed.
Signature · 7 lines
Alexandre Belloni, Free Electrons Embedded Linux and Kernel engineering http://free-electrons.com -- 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
[email protected]
maintainerPATCH v2 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
@@ -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
[email protected]
maintainerPATCH v2 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
@@ -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-mt7622.c ARM/Mediatek SoC support M: Matthias Brugger <[email protected]>
Signature · 1 line
2.7.4