* Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
From: Sean Wang @ 2017-10-19 2:18 UTC (permalink / raw)
To: Yingjoe Chen
Cc: a.zummo, alexandre.belloni, robh+dt, mark.rutland, linux-rtc,
devicetree, linux-mediatek, linux-kernel
In-Reply-To: <1508320354.13340.19.camel@mtksdaap41>
On Wed, 2017-10-18 at 17:52 +0800, Yingjoe Chen wrote:
> On Tue, 2017-10-17 at 17:40 +0800, sean.wang@mediatek.com wrote:
> > From: Sean Wang <sean.wang@mediatek.com>
> >
> > This patch introduces the driver for the RTC on MT7622 SoC.
> >
> > Signed-off-by: Sean Wang <sean.wang@mediatek.com>
> > ---
> > 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
> >
> > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > index e0e58f3..4226295 100644
> > --- a/drivers/rtc/Kconfig
> > +++ b/drivers/rtc/Kconfig
> > @@ -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)
> > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> > index 7230014..593a02c 100644
> > --- a/drivers/rtc/Makefile
> > +++ b/drivers/rtc/Makefile
> > @@ -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
> > diff --git a/drivers/rtc/rtc-mt7622.c b/drivers/rtc/rtc-mt7622.c
> > new file mode 100644
> > index 0000000..1f00494
> > --- /dev/null
> > +++ b/drivers/rtc/rtc-mt7622.c
> > @@ -0,0 +1,418 @@
> > +/*
> > + * Driver for MediaTek SoC based RTC
> > + *
> > + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
> > + *
> > + * 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.
>
> 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
>
Nice catch. It will be fixed. Thanks for your careful review.
Sean
> > + */
> > + 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 <sean.wang@mediatek.com>");
> > +MODULE_LICENSE("GPL");
>
>
^ permalink raw reply
* Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
From: Sean Wang @ 2017-10-19 2:55 UTC (permalink / raw)
To: Alexandre Belloni, Yingjoe Chen
Cc: a.zummo, robh+dt, mark.rutland, linux-rtc, devicetree,
linux-mediatek, linux-kernel
In-Reply-To: <20171018125743.kqna6b4lgng674om@piout.net>
Hi, both
On Wed, 2017-10-18 at 14:57 +0200, Alexandre Belloni wrote:
> On 18/10/2017 at 19:12:06 +0800, Yingjoe Chen wrote:
> > On Tue, 2017-10-17 at 17:40 +0800, sean.wang@mediatek.com wrote:
> > > From: Sean Wang <sean.wang@mediatek.com>
> > >
> > > This patch introduces the driver for the RTC on MT7622 SoC.
> > >
> > > Signed-off-by: Sean Wang <sean.wang@mediatek.com>
> > > ---
> > > 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
> > >
> > > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > > index e0e58f3..4226295 100644
> > > --- a/drivers/rtc/Kconfig
> > > +++ b/drivers/rtc/Kconfig
> > > @@ -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.
> >
>
> 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.
> > > + 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.
> >
>
> 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
^ permalink raw reply
* Re: [PATCH v2 1/4] dt-bindings: rtc: mediatek: add bindings for MediaTek SoC based RTC
From: Sean Wang @ 2017-10-19 3:00 UTC (permalink / raw)
To: Yingjoe Chen
Cc: a.zummo, alexandre.belloni, robh+dt, mark.rutland, linux-rtc,
devicetree, linux-mediatek, linux-kernel
In-Reply-To: <1508333529.13340.26.camel@mtksdaap41>
On Wed, 2017-10-18 at 21:32 +0800, Yingjoe Chen wrote:
> On Tue, 2017-10-17 at 17:40 +0800, sean.wang@mediatek.com wrote:
> > From: Sean Wang <sean.wang@mediatek.com>
> >
> > Add device-tree binding for MediaTek SoC based RTC
> >
> > Cc: devicetree@vger.kernel.org
> > Signed-off-by: Sean Wang <sean.wang@mediatek.com>
> > Acked-by: Rob Herring <robh@kernel.org>
> > ---
> > .../devicetree/bindings/rtc/rtc-mediatek.txt | 21 +++++++++++++++++++++
> > 1 file changed, 21 insertions(+)
> > create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
> >
> > diff --git a/Documentation/devicetree/bindings/rtc/rtc-mediatek.txt b/Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
> > new file mode 100644
> > index 0000000..09fe8f5
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
>
>
> How about change this filename to match driver filename change?
>
> Joe.C
>
Will be improved together with the other patches
^ permalink raw reply
* Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
From: Alexandre Belloni @ 2017-10-19 9:02 UTC (permalink / raw)
To: Sean Wang
Cc: Yingjoe Chen, a.zummo, robh+dt, mark.rutland, linux-rtc,
devicetree, linux-mediatek, linux-kernel
In-Reply-To: <1508381749.21840.123.camel@mtkswgap22>
On 19/10/2017 at 10:55:49 +0800, Sean Wang wrote:
> Hi, both
>
> On Wed, 2017-10-18 at 14:57 +0200, Alexandre Belloni wrote:
> > On 18/10/2017 at 19:12:06 +0800, Yingjoe Chen wrote:
> > > On Tue, 2017-10-17 at 17:40 +0800, sean.wang@mediatek.com wrote:
> > > > From: Sean Wang <sean.wang@mediatek.com>
> > > >
> > > > This patch introduces the driver for the RTC on MT7622 SoC.
> > > >
> > > > Signed-off-by: Sean Wang <sean.wang@mediatek.com>
> > > > ---
> > > > 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
> > > >
> > > > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > > > index e0e58f3..4226295 100644
> > > > --- a/drivers/rtc/Kconfig
> > > > +++ b/drivers/rtc/Kconfig
> > > > @@ -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.
> > >
> >
> > 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.
>
>
> > > > + 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.
> > >
> >
> > 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.
>
So you can make it work from 2001 to 2100 but I'm not sure it is worth
it.
--
Alexandre Belloni, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
^ permalink raw reply
* Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
From: Sean Wang @ 2017-10-19 10:04 UTC (permalink / raw)
To: Alexandre Belloni
Cc: Yingjoe Chen, a.zummo, robh+dt, mark.rutland, linux-rtc,
devicetree, linux-mediatek, linux-kernel
In-Reply-To: <20171019090219.2odtq3xosqk5ytsb@piout.net>
On Thu, 2017-10-19 at 11:02 +0200, Alexandre Belloni wrote:
> On 19/10/2017 at 10:55:49 +0800, Sean Wang wrote:
> > Hi, both
> >
> > On Wed, 2017-10-18 at 14:57 +0200, Alexandre Belloni wrote:
> > > On 18/10/2017 at 19:12:06 +0800, Yingjoe Chen wrote:
> > > > On Tue, 2017-10-17 at 17:40 +0800, sean.wang@mediatek.com wrote:
> > > > > From: Sean Wang <sean.wang@mediatek.com>
> > > > >
> > > > > This patch introduces the driver for the RTC on MT7622 SoC.
> > > > >
> > > > > Signed-off-by: Sean Wang <sean.wang@mediatek.com>
> > > > > ---
> > > > > 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
> > > > >
> > > > > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > > > > index e0e58f3..4226295 100644
> > > > > --- a/drivers/rtc/Kconfig
> > > > > +++ b/drivers/rtc/Kconfig
> > > > > @@ -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.
> > > >
> > >
> > > 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.
> >
> >
> > > > > + 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.
> > > >
> > >
> > > 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.
> >
>
> 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.
^ permalink raw reply
* Re: [PATCH v2 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
From: Alexandre Belloni @ 2017-10-19 10:13 UTC (permalink / raw)
To: Sean Wang
Cc: Yingjoe Chen, a.zummo, robh+dt, mark.rutland, linux-rtc,
devicetree, linux-mediatek, linux-kernel
In-Reply-To: <1508407479.29850.5.camel@mtkswgap22>
On 19/10/2017 at 18:04:39 +0800, Sean Wang wrote:
> 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.
--
Alexandre Belloni, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
^ permalink raw reply
* Re: [PATCH V2] rtc: add support for NXP PCF85363 real-time clock
From: Rob Herring @ 2017-10-19 21:24 UTC (permalink / raw)
To: Eric Nelson
Cc: linux-rtc, Alessandro Zummo, Alexandre Belloni, Mark Rutland,
devicetree@vger.kernel.org, Otavio Salvador, Fabio Estevam
In-Reply-To: <20602538-2164-e85c-33b6-31c99dfa8a5e@nelint.com>
On Tue, Oct 17, 2017 at 5:24 PM, Eric Nelson <eric@nelint.com> wrote:
> Hi Rob,
>
>
> On 10/17/2017 01:06 PM, Rob Herring wrote:
>>
>> On Wed, Oct 11, 2017 at 08:56:30AM -0700, Eric Nelson wrote:
>>>
>>> Note that alarms are not currently implemented.
>>>
>>> 64 bytes of nvmem is supported and exposed in
>>> sysfs (# is the instance number, starting with 0):
>>>
>>> /sys/bus/nvmem/devices/pcf85363-#/nvmem
>>>
>>> Signed-off-by: Eric Nelson <eric@nelint.com>
>>> ---
>>> V2 addresses a couple of issues highlighted by Fabio Estevam
>>> 1. Kconfig updated to select REGMAP_I2C
>>> 2. Switch to of_device_id from i2c_device_id for driver matching
>>>
>>> Documentation/devicetree/bindings/rtc/pcf85363.txt | 16 ++
>>> drivers/rtc/Kconfig | 13 ++
>>> drivers/rtc/Makefile | 1 +
>>> drivers/rtc/rtc-pcf85363.c | 221
>>> +++++++++++++++++++++
>>> 4 files changed, 251 insertions(+)
>>> create mode 100644 Documentation/devicetree/bindings/rtc/pcf85363.txt
>>> create mode 100644 drivers/rtc/rtc-pcf85363.c
>>>
>>> diff --git a/Documentation/devicetree/bindings/rtc/pcf85363.txt
>>> b/Documentation/devicetree/bindings/rtc/pcf85363.txt
>>> new file mode 100644
>>> index 0000000..5fddb9f
>>> --- /dev/null
>>> +++ b/Documentation/devicetree/bindings/rtc/pcf85363.txt
>>> @@ -0,0 +1,16 @@
>>> +NXP PCF85363 Real Time Clock
>>> +============================
>>> +
>>> +Required properties:
>>> +- compatible: Should contain "nxp,pcf85363".
>>> +- reg: I2C address for chip.
>>> +
>>> +Example:
>>> +
>>> +pcf85363: pcf85363@51 {
>>> + compatible = "nxp,pcf85363";
>>> + reg = <0x51>;
>>> +};
>>> +
>>> +Note that alarms are not yet supported, so a specifier for
>>> +"interrupts" will be ignored.
>>
>>
>> Irrelevant to the binding. That's a driver feature (or lack of). If the
>> device has an interrupt line, you should document it. It can be optional
>> though.
>>
>
> That's what I was attempting do do in an awkward way.
>
> Since the driver doesn't support interrupts or alarms at the moment,
> I'll remove this note.
>
> The chip does have an interrupt pin, but the board I have doesn't
> have it connected.
Then just document "interrupts" as optional.
Rob
^ permalink raw reply
* [PATCH v3 0/4] rtc: mediatek: add support for SoC based RTC on MT7622
From: sean.wang @ 2017-10-23 7:16 UTC (permalink / raw)
To: a.zummo, alexandre.belloni, robh+dt, mark.rutland
Cc: linux-rtc, devicetree, linux-mediatek, linux-kernel, Sean Wang
From: Sean Wang <sean.wang@mediatek.com>
Changes since v2:
- Remove time extension with yr_base
- Add fixup that ensures all time fields in hardware keeping consistency
inside the mtk_rtc_get_alarm_or_time call
- Add validity for time range should be between 2001 to 2099
- Enhance comments with aligned style and explanation for certain limiation in
hardware
- Rename dt-binding naming from rtc-mediatek.txt to rtc-mt7622.txt
- Rename config naming from RTC_DRV_MEDIATEK to RTC_DRV_MT7622
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-mt7622.txt | 21 +
MAINTAINERS | 3 +
drivers/rtc/Kconfig | 18 +-
drivers/rtc/Makefile | 1 +
drivers/rtc/rtc-mt7622.c | 422 +++++++++++++++++++++
5 files changed, 461 insertions(+), 4 deletions(-)
create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mt7622.txt
create mode 100644 drivers/rtc/rtc-mt7622.c
--
2.7.4
^ permalink raw reply
* [PATCH v3 1/4] dt-bindings: rtc: mediatek: add bindings for MediaTek SoC based RTC
From: sean.wang @ 2017-10-23 7:16 UTC (permalink / raw)
To: a.zummo, alexandre.belloni, robh+dt, mark.rutland
Cc: linux-rtc, devicetree, linux-mediatek, linux-kernel, Sean Wang
In-Reply-To: <cover.1508741262.git.sean.wang@mediatek.com>
From: Sean Wang <sean.wang@mediatek.com>
Add device-tree binding for MediaTek SoC based RTC
Cc: devicetree@vger.kernel.org
Signed-off-by: Sean Wang <sean.wang@mediatek.com>
Acked-by: Rob Herring <robh@kernel.org>
---
.../devicetree/bindings/rtc/rtc-mt7622.txt | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mt7622.txt
diff --git a/Documentation/devicetree/bindings/rtc/rtc-mt7622.txt b/Documentation/devicetree/bindings/rtc/rtc-mt7622.txt
new file mode 100644
index 0000000..09fe8f5
--- /dev/null
+++ b/Documentation/devicetree/bindings/rtc/rtc-mt7622.txt
@@ -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";
+};
--
2.7.4
^ permalink raw reply related
* [PATCH v3 3/4] rtc: mediatek: enhance the description for MediaTek PMIC based RTC
From: sean.wang @ 2017-10-23 7:16 UTC (permalink / raw)
To: a.zummo, alexandre.belloni, robh+dt, mark.rutland
Cc: linux-rtc, devicetree, linux-mediatek, linux-kernel, Sean Wang,
Eddie Huang
In-Reply-To: <cover.1508741262.git.sean.wang@mediatek.com>
From: Sean Wang <sean.wang@mediatek.com>
Give a better description for original MediaTek RTC driver as PMIC based
RTC in order to distinguish SoC based RTC. Also turning all words with
Mediatek to MediaTek here.
Cc: Eddie Huang <eddie.huang@mediatek.com>
Signed-off-by: Sean Wang <sean.wang@mediatek.com>
Acked-by: Eddie Huang <eddie.huang@mediatek.com>
---
drivers/rtc/Kconfig | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index fd9b590..6a315cb 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -1716,14 +1716,14 @@ config RTC_DRV_MT7622
will be called rtc-mt7622.
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"
--
2.7.4
^ permalink raw reply related
* [PATCH v3 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
From: sean.wang @ 2017-10-23 7:16 UTC (permalink / raw)
To: a.zummo, alexandre.belloni, robh+dt, mark.rutland
Cc: linux-rtc, devicetree, linux-mediatek, linux-kernel, Sean Wang
In-Reply-To: <cover.1508741262.git.sean.wang@mediatek.com>
From: Sean Wang <sean.wang@mediatek.com>
This patch introduces the driver for the RTC on MT7622 SoC.
Signed-off-by: Sean Wang <sean.wang@mediatek.com>
---
drivers/rtc/Kconfig | 10 ++
drivers/rtc/Makefile | 1 +
drivers/rtc/rtc-mt7622.c | 422 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 433 insertions(+)
create mode 100644 drivers/rtc/rtc-mt7622.c
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index e0e58f3..fd9b590 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -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_MT7622
+ 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-mt7622.
+
config RTC_DRV_MT6397
tristate "Mediatek Real Time Clock driver"
depends on MFD_MT6397 || (COMPILE_TEST && IRQ_DOMAIN)
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 7230014..eb47f8c 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -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_MT7622) += 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
diff --git a/drivers/rtc/rtc-mt7622.c b/drivers/rtc/rtc-mt7622.c
new file mode 100644
index 0000000..d79b9ae
--- /dev/null
+++ b/drivers/rtc/rtc-mt7622.c
@@ -0,0 +1,422 @@
+/*
+ * Driver for MediaTek SoC based RTC
+ *
+ * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
+ *
+ * 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)
+
+/*
+ * The offset is used in the translation for the year between in struct
+ * rtc_time and in hardware register MTK_RTC_TREG(x,MTK_YEA)
+ */
+#define MTK_RTC_TM_YR_OFFSET 100
+
+/*
+ * The lowest value for the valid tm_year. RTC hardware would take incorrectly
+ * tm_year 100 as not a leap year and thus it is also required being excluded
+ * from the valid options.
+ */
+#define MTK_RTC_TM_YR_L (MTK_RTC_TM_YR_OFFSET + 1)
+
+/*
+ * The most year the RTC can hold is 99 and the next to 99 in year register
+ * would be wraparound to 0, for MT7622.
+ */
+#define MTK_RTC_HW_YR_LIMIT 99
+
+/* The highest value for the valid tm_year */
+#define MTK_RTC_TM_YR_H (MTK_RTC_TM_YR_OFFSET + MTK_RTC_HW_YR_LIMIT)
+
+/* Simple macro helps to check whether the hardware supports the tm_year */
+#define MTK_RTC_TM_YR_VALID(_y) ((_y) >= MTK_RTC_TM_YR_L && \
+ (_y) <= MTK_RTC_TM_YR_H)
+
+/* Types of the function the RTC provides are time counter and alarm. */
+enum {
+ MTK_TC,
+ MTK_AL,
+};
+
+/* 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;
+};
+
+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. Note that MTK_SEC must
+ * be read first. In this way, it guarantees the others remain not
+ * changed when the results for two MTK_SEC consecutive reads are same.
+ */
+ do {
+ sec = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_SEC));
+ min = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MIN));
+ hour = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_HOU));
+ wday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOW));
+ mday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOM));
+ mon = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MON));
+ year = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_YEA));
+ } 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 queries */
+ tm->tm_year = year + MTK_RTC_TM_YR_OFFSET;
+}
+
+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_OFFSET;
+
+ 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);
+
+ if (!MTK_RTC_TM_YR_VALID(tm->tm_year))
+ return -EINVAL;
+
+ /* 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;
+
+ if (!MTK_RTC_TM_YR_VALID(alrm_tm->tm_year))
+ return -EINVAL;
+
+ /*
+ * 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 <sean.wang@mediatek.com>");
+MODULE_LICENSE("GPL");
--
2.7.4
^ permalink raw reply related
* [PATCH v3 4/4] rtc: mediatek: update MAINTAINERS entry with MediaTek RTC driver
From: sean.wang @ 2017-10-23 7:16 UTC (permalink / raw)
To: a.zummo, alexandre.belloni, robh+dt, mark.rutland
Cc: linux-rtc, devicetree, linux-mediatek, linux-kernel, Sean Wang,
Eddie Huang
In-Reply-To: <cover.1508741262.git.sean.wang@mediatek.com>
From: Sean Wang <sean.wang@mediatek.com>
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.
Cc: Eddie Huang <eddie.huang@mediatek.com>
Signed-off-by: Sean Wang <sean.wang@mediatek.com>
---
MAINTAINERS | 3 +++
1 file changed, 3 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 2281af4..332c04c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1579,10 +1579,13 @@ F: drivers/rtc/rtc-armada38x.c
ARM/Mediatek RTC DRIVER
M: Eddie Huang <eddie.huang@mediatek.com>
+M: Sean Wang <sean.wang@mediatek.com>
L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
L: linux-mediatek@lists.infradead.org (moderated for non-subscribers)
S: Maintained
+F: Documentation/devicetree/bindings/rtc/rtc-mt7622.txt
F: drivers/rtc/rtc-mt6397.c
+F: drivers/rtc/rtc-mt7622.c
ARM/Mediatek SoC support
M: Matthias Brugger <matthias.bgg@gmail.com>
--
2.7.4
^ permalink raw reply related
* Re: [RFC v6 8/9] platform/x86: intel_pmc_ipc: Use generic Intel IPC device calls
From: Lee Jones @ 2017-10-23 15:45 UTC (permalink / raw)
To: sathyanarayanan.kuppuswamy
Cc: a.zummo, x86, wim, mingo, alexandre.belloni, qipeng.zha, hpa,
dvhart, tglx, andy, souvik.k.chakravarty, linux-rtc,
linux-watchdog, linux-kernel, platform-driver-x86, sathyaosid
In-Reply-To: <491e7ea183ada1e50ac4ece6ca3f73ccda9cd42e.1508368279.git.sathyanarayanan.kuppuswamy@linux.intel.com>
On Wed, 18 Oct 2017, sathyanarayanan.kuppuswamy@linux.intel.com wrote:
> From: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
>
> Removed redundant IPC helper functions and refactored the driver to use
> generic IPC device driver APIs. Also, cleaned up the driver to minimize
> the usage of global variable ipcdev by propogating the struct
> intel_pmc_ipc_dev pointer or by getting it from device private data.
>
> This patch also cleans-up PMC IPC user drivers(intel_telemetry_pltdrv.c,
> intel_soc_pmic_bxtwc.c) to use APIs provided by generic IPC driver.
>
> Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
> ---
> arch/x86/include/asm/intel_pmc_ipc.h | 31 +-
> drivers/mfd/intel_soc_pmic_bxtwc.c | 37 ++-
> drivers/platform/x86/intel_pmc_ipc.c | 393 ++++++++++----------------
> drivers/platform/x86/intel_telemetry_pltdrv.c | 128 ++++-----
> include/linux/mfd/intel_soc_pmic.h | 2 +
Acked-by: Lee Jones <lee.jones@linaro.org>
--
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
^ permalink raw reply
* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
From: Chen Zhong @ 2017-10-24 1:27 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1506509048-19032-6-git-send-email-chen.zhong@mediatek.com>
Just gentle ping.
On Wed, 2017-09-27 at 18:44 +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
>
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
> drivers/input/keyboard/Kconfig | 9 +
> drivers/input/keyboard/Makefile | 1 +
> drivers/input/keyboard/mtk-pmic-keys.c | 341 ++++++++++++++++++++++++++++++++
> 3 files changed, 351 insertions(+)
> create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
>
> diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> index 4c4ab1c..bd4e20a 100644
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> To compile this driver as a module, choose M here: the
> module will be called bcm-keypad.
>
> +config KEYBOARD_MTK_PMIC
> + tristate "MediaTek PMIC keys support"
> + depends on MFD_MT6397
> + help
> + Say Y here if you want to use the pmic keys (powerkey/homekey).
> +
> + To compile this driver as a module, choose M here: the
> + module will be called pmic-keys.
> +
> endif
> diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> index d2338ba..20c0b98 100644
> --- a/drivers/input/keyboard/Makefile
> +++ b/drivers/input/keyboard/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
> obj-$(CONFIG_KEYBOARD_MAX7359) += max7359_keypad.o
> obj-$(CONFIG_KEYBOARD_MCS) += mcs_touchkey.o
> obj-$(CONFIG_KEYBOARD_MPR121) += mpr121_touchkey.o
> +obj-$(CONFIG_KEYBOARD_MTK_PMIC) += mtk-pmic-keys.o
> obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
> obj-$(CONFIG_KEYBOARD_NOMADIK) += nomadik-ske-keypad.o
> obj-$(CONFIG_KEYBOARD_NSPIRE) += nspire-keypad.o
> diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> new file mode 100644
> index 0000000..529fd95
> --- /dev/null
> +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> @@ -0,0 +1,341 @@
> +/*
> + * Copyright (C) 2017 MediaTek, Inc.
> + *
> + * Author: Chen Zhong <chen.zhong@mediatek.com>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * 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/module.h>
> +#include <linux/kernel.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/kernel.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/regmap.h>
> +#include <linux/mfd/mt6323/registers.h>
> +#include <linux/mfd/mt6397/registers.h>
> +#include <linux/mfd/mt6397/core.h>
> +
> +#define MTK_PMIC_PWRKEY_RST_EN_MASK 0x1
> +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT 6
> +#define MTK_PMIC_HOMEKEY_RST_EN_MASK 0x1
> +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT 5
> +#define MTK_PMIC_RST_DU_MASK 0x3
> +#define MTK_PMIC_RST_DU_SHIFT 8
> +
> +#define MTK_PMIC_PWRKEY_RST \
> + (MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> +#define MTK_PMIC_HOMEKEY_RST \
> + (MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> +
> +#define MTK_PMIC_PWRKEY_INDEX 0
> +#define MTK_PMIC_HOMEKEY_INDEX 1
> +#define MTK_PMIC_MAX_KEY_COUNT 2
> +
> +struct mtk_pmic_keys_regs {
> + u32 deb_reg;
> + u32 deb_mask;
> + u32 intsel_reg;
> + u32 intsel_mask;
> +};
> +
> +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask, \
> + _intsel_reg, _intsel_mask) \
> +{ \
> + .deb_reg = _deb_reg, \
> + .deb_mask = _deb_mask, \
> + .intsel_reg = _intsel_reg, \
> + .intsel_mask = _intsel_mask, \
> +}
> +
> +struct mtk_pmic_regs {
> + const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> + u32 pmic_rst_reg;
> +};
> +
> +static const struct mtk_pmic_regs mt6397_regs = {
> + .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> + MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> + 0x8, MT6397_INT_RSV, 0x10),
> + .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> + MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> + 0x10, MT6397_INT_RSV, 0x8),
> + .pmic_rst_reg = MT6397_TOP_RST_MISC,
> +};
> +
> +static const struct mtk_pmic_regs mt6323_regs = {
> + .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> + MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> + 0x2, MT6323_INT_MISC_CON, 0x10),
> + .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> + MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> + 0x4, MT6323_INT_MISC_CON, 0x8),
> + .pmic_rst_reg = MT6323_TOP_RST_MISC,
> +};
> +
> +struct mtk_pmic_keys_info {
> + struct mtk_pmic_keys *keys;
> + const struct mtk_pmic_keys_regs *regs;
> + unsigned int keycode;
> + int irq;
> + bool wakeup:1;
> +};
> +
> +struct mtk_pmic_keys {
> + struct input_dev *input_dev;
> + struct device *dev;
> + struct regmap *regmap;
> + struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> +};
> +
> +enum mtk_pmic_keys_lp_mode {
> + LP_DISABLE,
> + LP_ONEKEY,
> + LP_TWOKEY,
> +};
> +
> +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> + u32 pmic_rst_reg)
> +{
> + int ret;
> + u32 long_press_mode, long_press_debounce;
> +
> + ret = of_property_read_u32(keys->dev->of_node,
> + "power-off-time-sec", &long_press_debounce);
> + if (ret)
> + long_press_debounce = 0;
> +
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> + long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> +
> + ret = of_property_read_u32(keys->dev->of_node,
> + "mediatek,long-press-mode", &long_press_mode);
> + if (ret)
> + long_press_mode = LP_DISABLE;
> +
> + switch (long_press_mode) {
> + case LP_ONEKEY:
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_PWRKEY_RST,
> + MTK_PMIC_PWRKEY_RST);
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_HOMEKEY_RST,
> + 0);
> + break;
> + case LP_TWOKEY:
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_PWRKEY_RST,
> + MTK_PMIC_PWRKEY_RST);
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_HOMEKEY_RST,
> + MTK_PMIC_HOMEKEY_RST);
> + break;
> + case LP_DISABLE:
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_PWRKEY_RST,
> + 0);
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_HOMEKEY_RST,
> + 0);
> + break;
> + default:
> + break;
> + }
> +}
> +
> +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> +{
> + struct mtk_pmic_keys_info *info = data;
> + u32 key_deb, pressed;
> +
> + regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> +
> + key_deb &= info->regs->deb_mask;
> +
> + pressed = !key_deb;
> +
> + input_report_key(info->keys->input_dev, info->keycode, pressed);
> + input_sync(info->keys->input_dev);
> +
> + dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> + pressed ? "pressed" : "released", info->keycode);
> +
> + return IRQ_HANDLED;
> +}
> +
> +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> + struct mtk_pmic_keys_info *info)
> +{
> + int ret;
> +
> + info->keys = keys;
> +
> + ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> + info->regs->intsel_mask,
> + info->regs->intsel_mask);
> + if (ret < 0)
> + return ret;
> +
> + ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> + mtk_pmic_keys_irq_handler_thread,
> + IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> + "mtk-pmic-keys", info);
> + if (ret) {
> + dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> + info->irq, ret);
> + return ret;
> + }
> +
> + input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> +
> + return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mtk_pmic_keys_suspend(struct device *dev)
> +{
> + struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> + int index;
> +
> + for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> + if (keys->keys[index].wakeup)
> + enable_irq_wake(keys->keys[index].irq);
> + }
> +
> + return 0;
> +}
> +
> +static int mtk_pmic_keys_resume(struct device *dev)
> +{
> + struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> + int index;
> +
> + for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> + if (keys->keys[index].wakeup)
> + disable_irq_wake(keys->keys[index].irq);
> + }
> +
> + return 0;
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> + mtk_pmic_keys_resume);
> +
> +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> + {
> + .compatible = "mediatek,mt6397-keys",
> + .data = &mt6397_regs,
> + }, {
> + .compatible = "mediatek,mt6323-keys",
> + .data = &mt6323_regs,
> + }, {
> + /* sentinel */
> + }
> +};
> +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> +
> +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> +{
> + int error, index = 0;
> + unsigned int keycount;
> + struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> + struct device_node *node = pdev->dev.of_node, *child;
> + struct mtk_pmic_keys *keys;
> + const struct mtk_pmic_regs *mtk_pmic_regs;
> + struct input_dev *input_dev;
> + const struct of_device_id *of_id =
> + of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> +
> + keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> + if (!keys)
> + return -ENOMEM;
> +
> + keys->dev = &pdev->dev;
> + keys->regmap = pmic_chip->regmap;
> + mtk_pmic_regs = of_id->data;
> +
> + keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> + if (!input_dev) {
> + dev_err(keys->dev, "input allocate device fail.\n");
> + return -ENOMEM;
> + }
> +
> + input_dev->name = "mtk-pmic-keys";
> + input_dev->id.bustype = BUS_HOST;
> + input_dev->id.vendor = 0x0001;
> + input_dev->id.product = 0x0001;
> + input_dev->id.version = 0x0001;
> +
> + keycount = device_get_child_node_count(keys->dev);
> + if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> + dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> + return -EINVAL;
> + }
> +
> + for_each_child_of_node(node, child) {
> + keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> +
> + keys->keys[index].irq = platform_get_irq(pdev, index);
> + if (keys->keys[index].irq < 0)
> + return keys->keys[index].irq;
> +
> + error = of_property_read_u32(child,
> + "linux,keycodes", &keys->keys[index].keycode);
> + if (error) {
> + dev_err(keys->dev,
> + "failed to read key:%d linux,keycode property: %d\n",
> + index, error);
> + return error;
> + }
> +
> + if (of_property_read_bool(child, "wakeup-source"))
> + keys->keys[index].wakeup = true;
> +
> + error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> + if (error)
> + return error;
> +
> + index++;
> + }
> +
> + error = input_register_device(input_dev);
> + if (error) {
> + dev_err(&pdev->dev,
> + "register input device failed (%d)\n", error);
> + return error;
> + }
> +
> + mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> +
> + platform_set_drvdata(pdev, keys);
> +
> + return 0;
> +}
> +
> +static struct platform_driver pmic_keys_pdrv = {
> + .probe = mtk_pmic_keys_probe,
> + .driver = {
> + .name = "mtk-pmic-keys",
> + .of_match_table = of_mtk_pmic_keys_match_tbl,
> + .pm = &mtk_pmic_keys_pm_ops,
> + },
> +};
> +
> +module_platform_driver(pmic_keys_pdrv);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
^ permalink raw reply
* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
From: Dmitry Torokhov @ 2017-10-24 5:44 UTC (permalink / raw)
To: Chen Zhong
Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1506509048-19032-6-git-send-email-chen.zhong@mediatek.com>
On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> interrupts including pwrkey and homekey, also add setting for
> long press key shutdown behavior.
>
> Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> ---
> drivers/input/keyboard/Kconfig | 9 +
> drivers/input/keyboard/Makefile | 1 +
> drivers/input/keyboard/mtk-pmic-keys.c | 341 ++++++++++++++++++++++++++++++++
> 3 files changed, 351 insertions(+)
> create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
>
> diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> index 4c4ab1c..bd4e20a 100644
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> To compile this driver as a module, choose M here: the
> module will be called bcm-keypad.
>
> +config KEYBOARD_MTK_PMIC
> + tristate "MediaTek PMIC keys support"
> + depends on MFD_MT6397
> + help
> + Say Y here if you want to use the pmic keys (powerkey/homekey).
> +
> + To compile this driver as a module, choose M here: the
> + module will be called pmic-keys.
> +
> endif
> diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> index d2338ba..20c0b98 100644
> --- a/drivers/input/keyboard/Makefile
> +++ b/drivers/input/keyboard/Makefile
> @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
> obj-$(CONFIG_KEYBOARD_MAX7359) += max7359_keypad.o
> obj-$(CONFIG_KEYBOARD_MCS) += mcs_touchkey.o
> obj-$(CONFIG_KEYBOARD_MPR121) += mpr121_touchkey.o
> +obj-$(CONFIG_KEYBOARD_MTK_PMIC) += mtk-pmic-keys.o
> obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
> obj-$(CONFIG_KEYBOARD_NOMADIK) += nomadik-ske-keypad.o
> obj-$(CONFIG_KEYBOARD_NSPIRE) += nspire-keypad.o
> diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> new file mode 100644
> index 0000000..529fd95
> --- /dev/null
> +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> @@ -0,0 +1,341 @@
> +/*
> + * Copyright (C) 2017 MediaTek, Inc.
> + *
> + * Author: Chen Zhong <chen.zhong@mediatek.com>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * 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/module.h>
> +#include <linux/kernel.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/kernel.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/regmap.h>
> +#include <linux/mfd/mt6323/registers.h>
> +#include <linux/mfd/mt6397/registers.h>
> +#include <linux/mfd/mt6397/core.h>
> +
> +#define MTK_PMIC_PWRKEY_RST_EN_MASK 0x1
> +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT 6
> +#define MTK_PMIC_HOMEKEY_RST_EN_MASK 0x1
> +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT 5
> +#define MTK_PMIC_RST_DU_MASK 0x3
> +#define MTK_PMIC_RST_DU_SHIFT 8
> +
> +#define MTK_PMIC_PWRKEY_RST \
> + (MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> +#define MTK_PMIC_HOMEKEY_RST \
> + (MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> +
> +#define MTK_PMIC_PWRKEY_INDEX 0
> +#define MTK_PMIC_HOMEKEY_INDEX 1
> +#define MTK_PMIC_MAX_KEY_COUNT 2
> +
> +struct mtk_pmic_keys_regs {
> + u32 deb_reg;
> + u32 deb_mask;
> + u32 intsel_reg;
> + u32 intsel_mask;
> +};
> +
> +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask, \
> + _intsel_reg, _intsel_mask) \
> +{ \
> + .deb_reg = _deb_reg, \
> + .deb_mask = _deb_mask, \
> + .intsel_reg = _intsel_reg, \
> + .intsel_mask = _intsel_mask, \
> +}
> +
> +struct mtk_pmic_regs {
> + const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> + u32 pmic_rst_reg;
> +};
> +
> +static const struct mtk_pmic_regs mt6397_regs = {
> + .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> + MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> + 0x8, MT6397_INT_RSV, 0x10),
> + .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> + MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> + 0x10, MT6397_INT_RSV, 0x8),
> + .pmic_rst_reg = MT6397_TOP_RST_MISC,
> +};
> +
> +static const struct mtk_pmic_regs mt6323_regs = {
> + .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> + MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> + 0x2, MT6323_INT_MISC_CON, 0x10),
> + .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> + MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> + 0x4, MT6323_INT_MISC_CON, 0x8),
> + .pmic_rst_reg = MT6323_TOP_RST_MISC,
> +};
> +
> +struct mtk_pmic_keys_info {
> + struct mtk_pmic_keys *keys;
> + const struct mtk_pmic_keys_regs *regs;
> + unsigned int keycode;
> + int irq;
> + bool wakeup:1;
> +};
> +
> +struct mtk_pmic_keys {
> + struct input_dev *input_dev;
> + struct device *dev;
> + struct regmap *regmap;
> + struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> +};
> +
> +enum mtk_pmic_keys_lp_mode {
> + LP_DISABLE,
> + LP_ONEKEY,
> + LP_TWOKEY,
> +};
> +
> +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> + u32 pmic_rst_reg)
> +{
> + int ret;
> + u32 long_press_mode, long_press_debounce;
> +
> + ret = of_property_read_u32(keys->dev->of_node,
> + "power-off-time-sec", &long_press_debounce);
> + if (ret)
> + long_press_debounce = 0;
> +
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> + long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> +
> + ret = of_property_read_u32(keys->dev->of_node,
> + "mediatek,long-press-mode", &long_press_mode);
> + if (ret)
> + long_press_mode = LP_DISABLE;
> +
> + switch (long_press_mode) {
> + case LP_ONEKEY:
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_PWRKEY_RST,
> + MTK_PMIC_PWRKEY_RST);
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_HOMEKEY_RST,
> + 0);
> + break;
> + case LP_TWOKEY:
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_PWRKEY_RST,
> + MTK_PMIC_PWRKEY_RST);
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_HOMEKEY_RST,
> + MTK_PMIC_HOMEKEY_RST);
> + break;
> + case LP_DISABLE:
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_PWRKEY_RST,
> + 0);
> + regmap_update_bits(keys->regmap, pmic_rst_reg,
> + MTK_PMIC_HOMEKEY_RST,
> + 0);
> + break;
> + default:
> + break;
> + }
> +}
> +
> +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> +{
> + struct mtk_pmic_keys_info *info = data;
> + u32 key_deb, pressed;
> +
> + regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> +
> + key_deb &= info->regs->deb_mask;
> +
> + pressed = !key_deb;
> +
> + input_report_key(info->keys->input_dev, info->keycode, pressed);
> + input_sync(info->keys->input_dev);
> +
> + dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> + pressed ? "pressed" : "released", info->keycode);
> +
> + return IRQ_HANDLED;
> +}
> +
> +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> + struct mtk_pmic_keys_info *info)
> +{
> + int ret;
> +
> + info->keys = keys;
> +
> + ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> + info->regs->intsel_mask,
> + info->regs->intsel_mask);
> + if (ret < 0)
> + return ret;
> +
> + ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> + mtk_pmic_keys_irq_handler_thread,
> + IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> + "mtk-pmic-keys", info);
> + if (ret) {
> + dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> + info->irq, ret);
> + return ret;
> + }
> +
> + input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> +
> + return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mtk_pmic_keys_suspend(struct device *dev)
Please use __maybe_unused annotation instead of #ifdef guard.
> +{
> + struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> + int index;
> +
> + for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> + if (keys->keys[index].wakeup)
> + enable_irq_wake(keys->keys[index].irq);
> + }
> +
> + return 0;
> +}
> +
> +static int mtk_pmic_keys_resume(struct device *dev)
__maybe_unused here as well.
> +{
> + struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> + int index;
> +
> + for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> + if (keys->keys[index].wakeup)
> + disable_irq_wake(keys->keys[index].irq);
> + }
> +
> + return 0;
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> + mtk_pmic_keys_resume);
> +
> +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> + {
> + .compatible = "mediatek,mt6397-keys",
> + .data = &mt6397_regs,
> + }, {
> + .compatible = "mediatek,mt6323-keys",
> + .data = &mt6323_regs,
> + }, {
> + /* sentinel */
> + }
> +};
> +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> +
> +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> +{
> + int error, index = 0;
> + unsigned int keycount;
> + struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> + struct device_node *node = pdev->dev.of_node, *child;
> + struct mtk_pmic_keys *keys;
> + const struct mtk_pmic_regs *mtk_pmic_regs;
> + struct input_dev *input_dev;
> + const struct of_device_id *of_id =
> + of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> +
> + keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> + if (!keys)
> + return -ENOMEM;
> +
> + keys->dev = &pdev->dev;
> + keys->regmap = pmic_chip->regmap;
> + mtk_pmic_regs = of_id->data;
> +
> + keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> + if (!input_dev) {
> + dev_err(keys->dev, "input allocate device fail.\n");
> + return -ENOMEM;
> + }
> +
> + input_dev->name = "mtk-pmic-keys";
> + input_dev->id.bustype = BUS_HOST;
> + input_dev->id.vendor = 0x0001;
> + input_dev->id.product = 0x0001;
> + input_dev->id.version = 0x0001;
> +
> + keycount = device_get_child_node_count(keys->dev);
Since you are using of_* API everywhere else I'd rather we used
of_get_available_child_count() here.
Once these 2 issues are fixed please feel free to add
Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
I assume it will all go though MFD tree, right?
> + if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> + dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> + return -EINVAL;
> + }
> +
> + for_each_child_of_node(node, child) {
> + keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> +
> + keys->keys[index].irq = platform_get_irq(pdev, index);
> + if (keys->keys[index].irq < 0)
> + return keys->keys[index].irq;
> +
> + error = of_property_read_u32(child,
> + "linux,keycodes", &keys->keys[index].keycode);
> + if (error) {
> + dev_err(keys->dev,
> + "failed to read key:%d linux,keycode property: %d\n",
> + index, error);
> + return error;
> + }
> +
> + if (of_property_read_bool(child, "wakeup-source"))
> + keys->keys[index].wakeup = true;
> +
> + error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> + if (error)
> + return error;
> +
> + index++;
> + }
> +
> + error = input_register_device(input_dev);
> + if (error) {
> + dev_err(&pdev->dev,
> + "register input device failed (%d)\n", error);
> + return error;
> + }
> +
> + mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> +
> + platform_set_drvdata(pdev, keys);
> +
> + return 0;
> +}
> +
> +static struct platform_driver pmic_keys_pdrv = {
> + .probe = mtk_pmic_keys_probe,
> + .driver = {
> + .name = "mtk-pmic-keys",
> + .of_match_table = of_mtk_pmic_keys_match_tbl,
> + .pm = &mtk_pmic_keys_pm_ops,
> + },
> +};
> +
> +module_platform_driver(pmic_keys_pdrv);
> +
> +MODULE_LICENSE("GPL v2");
> +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
> --
> 1.7.9.5
>
Thanks.
--
Dmitry
^ permalink raw reply
* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
From: Chen Zhong @ 2017-10-24 9:37 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <20171024054409.6qbuzenzymkkjdth@dtor-ws>
On Mon, 2017-10-23 at 22:44 -0700, Dmitry Torokhov wrote:
> On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> > This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> > interrupts including pwrkey and homekey, also add setting for
> > long press key shutdown behavior.
> >
> > Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> > ---
> > drivers/input/keyboard/Kconfig | 9 +
> > drivers/input/keyboard/Makefile | 1 +
> > drivers/input/keyboard/mtk-pmic-keys.c | 341 ++++++++++++++++++++++++++++++++
> > 3 files changed, 351 insertions(+)
> > create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> >
> > diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> > index 4c4ab1c..bd4e20a 100644
> > --- a/drivers/input/keyboard/Kconfig
> > +++ b/drivers/input/keyboard/Kconfig
> > @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> > To compile this driver as a module, choose M here: the
> > module will be called bcm-keypad.
> >
> > +config KEYBOARD_MTK_PMIC
> > + tristate "MediaTek PMIC keys support"
> > + depends on MFD_MT6397
> > + help
> > + Say Y here if you want to use the pmic keys (powerkey/homekey).
> > +
> > + To compile this driver as a module, choose M here: the
> > + module will be called pmic-keys.
> > +
> > endif
> > diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> > index d2338ba..20c0b98 100644
> > --- a/drivers/input/keyboard/Makefile
> > +++ b/drivers/input/keyboard/Makefile
> > @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
> > obj-$(CONFIG_KEYBOARD_MAX7359) += max7359_keypad.o
> > obj-$(CONFIG_KEYBOARD_MCS) += mcs_touchkey.o
> > obj-$(CONFIG_KEYBOARD_MPR121) += mpr121_touchkey.o
> > +obj-$(CONFIG_KEYBOARD_MTK_PMIC) += mtk-pmic-keys.o
> > obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
> > obj-$(CONFIG_KEYBOARD_NOMADIK) += nomadik-ske-keypad.o
> > obj-$(CONFIG_KEYBOARD_NSPIRE) += nspire-keypad.o
> > diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> > new file mode 100644
> > index 0000000..529fd95
> > --- /dev/null
> > +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> > @@ -0,0 +1,341 @@
> > +/*
> > + * Copyright (C) 2017 MediaTek, Inc.
> > + *
> > + * Author: Chen Zhong <chen.zhong@mediatek.com>
> > + *
> > + * This software is licensed under the terms of the GNU General Public
> > + * License version 2, as published by the Free Software Foundation, and
> > + * may be copied, distributed, and modified under those terms.
> > + *
> > + * 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/module.h>
> > +#include <linux/kernel.h>
> > +#include <linux/input.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/kernel.h>
> > +#include <linux/of.h>
> > +#include <linux/of_device.h>
> > +#include <linux/regmap.h>
> > +#include <linux/mfd/mt6323/registers.h>
> > +#include <linux/mfd/mt6397/registers.h>
> > +#include <linux/mfd/mt6397/core.h>
> > +
> > +#define MTK_PMIC_PWRKEY_RST_EN_MASK 0x1
> > +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT 6
> > +#define MTK_PMIC_HOMEKEY_RST_EN_MASK 0x1
> > +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT 5
> > +#define MTK_PMIC_RST_DU_MASK 0x3
> > +#define MTK_PMIC_RST_DU_SHIFT 8
> > +
> > +#define MTK_PMIC_PWRKEY_RST \
> > + (MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> > +#define MTK_PMIC_HOMEKEY_RST \
> > + (MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> > +
> > +#define MTK_PMIC_PWRKEY_INDEX 0
> > +#define MTK_PMIC_HOMEKEY_INDEX 1
> > +#define MTK_PMIC_MAX_KEY_COUNT 2
> > +
> > +struct mtk_pmic_keys_regs {
> > + u32 deb_reg;
> > + u32 deb_mask;
> > + u32 intsel_reg;
> > + u32 intsel_mask;
> > +};
> > +
> > +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask, \
> > + _intsel_reg, _intsel_mask) \
> > +{ \
> > + .deb_reg = _deb_reg, \
> > + .deb_mask = _deb_mask, \
> > + .intsel_reg = _intsel_reg, \
> > + .intsel_mask = _intsel_mask, \
> > +}
> > +
> > +struct mtk_pmic_regs {
> > + const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> > + u32 pmic_rst_reg;
> > +};
> > +
> > +static const struct mtk_pmic_regs mt6397_regs = {
> > + .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > + MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> > + 0x8, MT6397_INT_RSV, 0x10),
> > + .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > + MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> > + 0x10, MT6397_INT_RSV, 0x8),
> > + .pmic_rst_reg = MT6397_TOP_RST_MISC,
> > +};
> > +
> > +static const struct mtk_pmic_regs mt6323_regs = {
> > + .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > + MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > + 0x2, MT6323_INT_MISC_CON, 0x10),
> > + .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > + MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > + 0x4, MT6323_INT_MISC_CON, 0x8),
> > + .pmic_rst_reg = MT6323_TOP_RST_MISC,
> > +};
> > +
> > +struct mtk_pmic_keys_info {
> > + struct mtk_pmic_keys *keys;
> > + const struct mtk_pmic_keys_regs *regs;
> > + unsigned int keycode;
> > + int irq;
> > + bool wakeup:1;
> > +};
> > +
> > +struct mtk_pmic_keys {
> > + struct input_dev *input_dev;
> > + struct device *dev;
> > + struct regmap *regmap;
> > + struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> > +};
> > +
> > +enum mtk_pmic_keys_lp_mode {
> > + LP_DISABLE,
> > + LP_ONEKEY,
> > + LP_TWOKEY,
> > +};
> > +
> > +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> > + u32 pmic_rst_reg)
> > +{
> > + int ret;
> > + u32 long_press_mode, long_press_debounce;
> > +
> > + ret = of_property_read_u32(keys->dev->of_node,
> > + "power-off-time-sec", &long_press_debounce);
> > + if (ret)
> > + long_press_debounce = 0;
> > +
> > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > + MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> > + long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> > +
> > + ret = of_property_read_u32(keys->dev->of_node,
> > + "mediatek,long-press-mode", &long_press_mode);
> > + if (ret)
> > + long_press_mode = LP_DISABLE;
> > +
> > + switch (long_press_mode) {
> > + case LP_ONEKEY:
> > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > + MTK_PMIC_PWRKEY_RST,
> > + MTK_PMIC_PWRKEY_RST);
> > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > + MTK_PMIC_HOMEKEY_RST,
> > + 0);
> > + break;
> > + case LP_TWOKEY:
> > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > + MTK_PMIC_PWRKEY_RST,
> > + MTK_PMIC_PWRKEY_RST);
> > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > + MTK_PMIC_HOMEKEY_RST,
> > + MTK_PMIC_HOMEKEY_RST);
> > + break;
> > + case LP_DISABLE:
> > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > + MTK_PMIC_PWRKEY_RST,
> > + 0);
> > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > + MTK_PMIC_HOMEKEY_RST,
> > + 0);
> > + break;
> > + default:
> > + break;
> > + }
> > +}
> > +
> > +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> > +{
> > + struct mtk_pmic_keys_info *info = data;
> > + u32 key_deb, pressed;
> > +
> > + regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> > +
> > + key_deb &= info->regs->deb_mask;
> > +
> > + pressed = !key_deb;
> > +
> > + input_report_key(info->keys->input_dev, info->keycode, pressed);
> > + input_sync(info->keys->input_dev);
> > +
> > + dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> > + pressed ? "pressed" : "released", info->keycode);
> > +
> > + return IRQ_HANDLED;
> > +}
> > +
> > +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> > + struct mtk_pmic_keys_info *info)
> > +{
> > + int ret;
> > +
> > + info->keys = keys;
> > +
> > + ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> > + info->regs->intsel_mask,
> > + info->regs->intsel_mask);
> > + if (ret < 0)
> > + return ret;
> > +
> > + ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> > + mtk_pmic_keys_irq_handler_thread,
> > + IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> > + "mtk-pmic-keys", info);
> > + if (ret) {
> > + dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> > + info->irq, ret);
> > + return ret;
> > + }
> > +
> > + input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> > +
> > + return 0;
> > +}
> > +
> > +#ifdef CONFIG_PM_SLEEP
> > +static int mtk_pmic_keys_suspend(struct device *dev)
>
> Please use __maybe_unused annotation instead of #ifdef guard.
>
> > +{
> > + struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > + int index;
> > +
> > + for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > + if (keys->keys[index].wakeup)
> > + enable_irq_wake(keys->keys[index].irq);
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static int mtk_pmic_keys_resume(struct device *dev)
>
> __maybe_unused here as well.
>
> > +{
> > + struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > + int index;
> > +
> > + for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > + if (keys->keys[index].wakeup)
> > + disable_irq_wake(keys->keys[index].irq);
> > + }
> > +
> > + return 0;
> > +}
> > +#endif
> > +
> > +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> > + mtk_pmic_keys_resume);
> > +
> > +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> > + {
> > + .compatible = "mediatek,mt6397-keys",
> > + .data = &mt6397_regs,
> > + }, {
> > + .compatible = "mediatek,mt6323-keys",
> > + .data = &mt6323_regs,
> > + }, {
> > + /* sentinel */
> > + }
> > +};
> > +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> > +
> > +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> > +{
> > + int error, index = 0;
> > + unsigned int keycount;
> > + struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> > + struct device_node *node = pdev->dev.of_node, *child;
> > + struct mtk_pmic_keys *keys;
> > + const struct mtk_pmic_regs *mtk_pmic_regs;
> > + struct input_dev *input_dev;
> > + const struct of_device_id *of_id =
> > + of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> > +
> > + keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> > + if (!keys)
> > + return -ENOMEM;
> > +
> > + keys->dev = &pdev->dev;
> > + keys->regmap = pmic_chip->regmap;
> > + mtk_pmic_regs = of_id->data;
> > +
> > + keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> > + if (!input_dev) {
> > + dev_err(keys->dev, "input allocate device fail.\n");
> > + return -ENOMEM;
> > + }
> > +
> > + input_dev->name = "mtk-pmic-keys";
> > + input_dev->id.bustype = BUS_HOST;
> > + input_dev->id.vendor = 0x0001;
> > + input_dev->id.product = 0x0001;
> > + input_dev->id.version = 0x0001;
> > +
> > + keycount = device_get_child_node_count(keys->dev);
>
> Since you are using of_* API everywhere else I'd rather we used
> of_get_available_child_count() here.
>
>
> Once these 2 issues are fixed please feel free to add
>
> Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
>
> I assume it will all go though MFD tree, right?
I will fix these 2 issues and send the new version.
Since patch 2,3,4,5 of this series are all for input driver, could you
help to merge them with the new version later?
Thank you.
>
> > + if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
> > + dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
> > + return -EINVAL;
> > + }
> > +
> > + for_each_child_of_node(node, child) {
> > + keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
> > +
> > + keys->keys[index].irq = platform_get_irq(pdev, index);
> > + if (keys->keys[index].irq < 0)
> > + return keys->keys[index].irq;
> > +
> > + error = of_property_read_u32(child,
> > + "linux,keycodes", &keys->keys[index].keycode);
> > + if (error) {
> > + dev_err(keys->dev,
> > + "failed to read key:%d linux,keycode property: %d\n",
> > + index, error);
> > + return error;
> > + }
> > +
> > + if (of_property_read_bool(child, "wakeup-source"))
> > + keys->keys[index].wakeup = true;
> > +
> > + error = mtk_pmic_key_setup(keys, &keys->keys[index]);
> > + if (error)
> > + return error;
> > +
> > + index++;
> > + }
> > +
> > + error = input_register_device(input_dev);
> > + if (error) {
> > + dev_err(&pdev->dev,
> > + "register input device failed (%d)\n", error);
> > + return error;
> > + }
> > +
> > + mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
> > +
> > + platform_set_drvdata(pdev, keys);
> > +
> > + return 0;
> > +}
> > +
> > +static struct platform_driver pmic_keys_pdrv = {
> > + .probe = mtk_pmic_keys_probe,
> > + .driver = {
> > + .name = "mtk-pmic-keys",
> > + .of_match_table = of_mtk_pmic_keys_match_tbl,
> > + .pm = &mtk_pmic_keys_pm_ops,
> > + },
> > +};
> > +
> > +module_platform_driver(pmic_keys_pdrv);
> > +
> > +MODULE_LICENSE("GPL v2");
> > +MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
> > +MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
> > --
> > 1.7.9.5
> >
>
> Thanks.
>
^ permalink raw reply
* Re: [PATCH v3 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC
From: Yingjoe Chen @ 2017-10-24 16:38 UTC (permalink / raw)
To: sean.wang
Cc: a.zummo, alexandre.belloni, robh+dt, mark.rutland, linux-rtc,
devicetree, linux-mediatek, linux-kernel
In-Reply-To: <daf20f5d249700320d6b1c1d2ed25fd1bd392ca3.1508741263.git.sean.wang@mediatek.com>
Sean,
Looks good to me. You can add if you want:
Reviewed-by: Yingjoe Chen <yingjoe.chen@mediatek.com>
Joe.C
On Mon, 2017-10-23 at 15:16 +0800, sean.wang@mediatek.com wrote:
> From: Sean Wang <sean.wang@mediatek.com>
>
> This patch introduces the driver for the RTC on MT7622 SoC.
>
> Signed-off-by: Sean Wang <sean.wang@mediatek.com>
> ---
> drivers/rtc/Kconfig | 10 ++
> drivers/rtc/Makefile | 1 +
> drivers/rtc/rtc-mt7622.c | 422 +++++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 433 insertions(+)
> create mode 100644 drivers/rtc/rtc-mt7622.c
>
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> index e0e58f3..fd9b590 100644
> --- a/drivers/rtc/Kconfig
> +++ b/drivers/rtc/Kconfig
> @@ -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_MT7622
> + 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-mt7622.
> +
> config RTC_DRV_MT6397
> tristate "Mediatek Real Time Clock driver"
> depends on MFD_MT6397 || (COMPILE_TEST && IRQ_DOMAIN)
> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> index 7230014..eb47f8c 100644
> --- a/drivers/rtc/Makefile
> +++ b/drivers/rtc/Makefile
> @@ -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_MT7622) += 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
> diff --git a/drivers/rtc/rtc-mt7622.c b/drivers/rtc/rtc-mt7622.c
> new file mode 100644
> index 0000000..d79b9ae
> --- /dev/null
> +++ b/drivers/rtc/rtc-mt7622.c
> @@ -0,0 +1,422 @@
> +/*
> + * Driver for MediaTek SoC based RTC
> + *
> + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
> + *
> + * 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)
> +
> +/*
> + * The offset is used in the translation for the year between in struct
> + * rtc_time and in hardware register MTK_RTC_TREG(x,MTK_YEA)
> + */
> +#define MTK_RTC_TM_YR_OFFSET 100
> +
> +/*
> + * The lowest value for the valid tm_year. RTC hardware would take incorrectly
> + * tm_year 100 as not a leap year and thus it is also required being excluded
> + * from the valid options.
> + */
> +#define MTK_RTC_TM_YR_L (MTK_RTC_TM_YR_OFFSET + 1)
> +
> +/*
> + * The most year the RTC can hold is 99 and the next to 99 in year register
> + * would be wraparound to 0, for MT7622.
> + */
> +#define MTK_RTC_HW_YR_LIMIT 99
> +
> +/* The highest value for the valid tm_year */
> +#define MTK_RTC_TM_YR_H (MTK_RTC_TM_YR_OFFSET + MTK_RTC_HW_YR_LIMIT)
> +
> +/* Simple macro helps to check whether the hardware supports the tm_year */
> +#define MTK_RTC_TM_YR_VALID(_y) ((_y) >= MTK_RTC_TM_YR_L && \
> + (_y) <= MTK_RTC_TM_YR_H)
> +
> +/* Types of the function the RTC provides are time counter and alarm. */
> +enum {
> + MTK_TC,
> + MTK_AL,
> +};
> +
> +/* 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;
> +};
> +
> +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. Note that MTK_SEC must
> + * be read first. In this way, it guarantees the others remain not
> + * changed when the results for two MTK_SEC consecutive reads are same.
> + */
> + do {
> + sec = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_SEC));
> + min = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MIN));
> + hour = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_HOU));
> + wday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOW));
> + mday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOM));
> + mon = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MON));
> + year = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_YEA));
> + } 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 queries */
> + tm->tm_year = year + MTK_RTC_TM_YR_OFFSET;
> +}
> +
> +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_OFFSET;
> +
> + 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);
> +
> + if (!MTK_RTC_TM_YR_VALID(tm->tm_year))
> + return -EINVAL;
> +
> + /* 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;
> +
> + if (!MTK_RTC_TM_YR_VALID(alrm_tm->tm_year))
> + return -EINVAL;
> +
> + /*
> + * 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 <sean.wang@mediatek.com>");
> +MODULE_LICENSE("GPL");
^ permalink raw reply
* Re: [PATCH v5 5/6] input: Add MediaTek PMIC keys support
From: Dmitry Torokhov @ 2017-10-24 19:16 UTC (permalink / raw)
To: Chen Zhong
Cc: Rob Herring, Lee Jones, Alexandre Belloni, Mark Rutland,
Matthias Brugger, Eddie Huang, Alessandro Zummo, Linus Walleij,
Chanwoo Choi, Jaechul Lee, Andi Shyti, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1508837852.4057.6.camel@mhfsdcap03>
On Tue, Oct 24, 2017 at 05:37:32PM +0800, Chen Zhong wrote:
> On Mon, 2017-10-23 at 22:44 -0700, Dmitry Torokhov wrote:
> > On Wed, Sep 27, 2017 at 06:44:07PM +0800, Chen Zhong wrote:
> > > This patch add support to handle MediaTek PMIC MT6397/MT6323 key
> > > interrupts including pwrkey and homekey, also add setting for
> > > long press key shutdown behavior.
> > >
> > > Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
> > > ---
> > > drivers/input/keyboard/Kconfig | 9 +
> > > drivers/input/keyboard/Makefile | 1 +
> > > drivers/input/keyboard/mtk-pmic-keys.c | 341 ++++++++++++++++++++++++++++++++
> > > 3 files changed, 351 insertions(+)
> > > create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
> > >
> > > diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
> > > index 4c4ab1c..bd4e20a 100644
> > > --- a/drivers/input/keyboard/Kconfig
> > > +++ b/drivers/input/keyboard/Kconfig
> > > @@ -756,4 +756,13 @@ config KEYBOARD_BCM
> > > To compile this driver as a module, choose M here: the
> > > module will be called bcm-keypad.
> > >
> > > +config KEYBOARD_MTK_PMIC
> > > + tristate "MediaTek PMIC keys support"
> > > + depends on MFD_MT6397
> > > + help
> > > + Say Y here if you want to use the pmic keys (powerkey/homekey).
> > > +
> > > + To compile this driver as a module, choose M here: the
> > > + module will be called pmic-keys.
> > > +
> > > endif
> > > diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
> > > index d2338ba..20c0b98 100644
> > > --- a/drivers/input/keyboard/Makefile
> > > +++ b/drivers/input/keyboard/Makefile
> > > @@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
> > > obj-$(CONFIG_KEYBOARD_MAX7359) += max7359_keypad.o
> > > obj-$(CONFIG_KEYBOARD_MCS) += mcs_touchkey.o
> > > obj-$(CONFIG_KEYBOARD_MPR121) += mpr121_touchkey.o
> > > +obj-$(CONFIG_KEYBOARD_MTK_PMIC) += mtk-pmic-keys.o
> > > obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
> > > obj-$(CONFIG_KEYBOARD_NOMADIK) += nomadik-ske-keypad.o
> > > obj-$(CONFIG_KEYBOARD_NSPIRE) += nspire-keypad.o
> > > diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
> > > new file mode 100644
> > > index 0000000..529fd95
> > > --- /dev/null
> > > +++ b/drivers/input/keyboard/mtk-pmic-keys.c
> > > @@ -0,0 +1,341 @@
> > > +/*
> > > + * Copyright (C) 2017 MediaTek, Inc.
> > > + *
> > > + * Author: Chen Zhong <chen.zhong@mediatek.com>
> > > + *
> > > + * This software is licensed under the terms of the GNU General Public
> > > + * License version 2, as published by the Free Software Foundation, and
> > > + * may be copied, distributed, and modified under those terms.
> > > + *
> > > + * 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/module.h>
> > > +#include <linux/kernel.h>
> > > +#include <linux/input.h>
> > > +#include <linux/interrupt.h>
> > > +#include <linux/platform_device.h>
> > > +#include <linux/kernel.h>
> > > +#include <linux/of.h>
> > > +#include <linux/of_device.h>
> > > +#include <linux/regmap.h>
> > > +#include <linux/mfd/mt6323/registers.h>
> > > +#include <linux/mfd/mt6397/registers.h>
> > > +#include <linux/mfd/mt6397/core.h>
> > > +
> > > +#define MTK_PMIC_PWRKEY_RST_EN_MASK 0x1
> > > +#define MTK_PMIC_PWRKEY_RST_EN_SHIFT 6
> > > +#define MTK_PMIC_HOMEKEY_RST_EN_MASK 0x1
> > > +#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT 5
> > > +#define MTK_PMIC_RST_DU_MASK 0x3
> > > +#define MTK_PMIC_RST_DU_SHIFT 8
> > > +
> > > +#define MTK_PMIC_PWRKEY_RST \
> > > + (MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
> > > +#define MTK_PMIC_HOMEKEY_RST \
> > > + (MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
> > > +
> > > +#define MTK_PMIC_PWRKEY_INDEX 0
> > > +#define MTK_PMIC_HOMEKEY_INDEX 1
> > > +#define MTK_PMIC_MAX_KEY_COUNT 2
> > > +
> > > +struct mtk_pmic_keys_regs {
> > > + u32 deb_reg;
> > > + u32 deb_mask;
> > > + u32 intsel_reg;
> > > + u32 intsel_mask;
> > > +};
> > > +
> > > +#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask, \
> > > + _intsel_reg, _intsel_mask) \
> > > +{ \
> > > + .deb_reg = _deb_reg, \
> > > + .deb_mask = _deb_mask, \
> > > + .intsel_reg = _intsel_reg, \
> > > + .intsel_mask = _intsel_mask, \
> > > +}
> > > +
> > > +struct mtk_pmic_regs {
> > > + const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
> > > + u32 pmic_rst_reg;
> > > +};
> > > +
> > > +static const struct mtk_pmic_regs mt6397_regs = {
> > > + .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > > + MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
> > > + 0x8, MT6397_INT_RSV, 0x10),
> > > + .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > > + MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
> > > + 0x10, MT6397_INT_RSV, 0x8),
> > > + .pmic_rst_reg = MT6397_TOP_RST_MISC,
> > > +};
> > > +
> > > +static const struct mtk_pmic_regs mt6323_regs = {
> > > + .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
> > > + MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > > + 0x2, MT6323_INT_MISC_CON, 0x10),
> > > + .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
> > > + MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
> > > + 0x4, MT6323_INT_MISC_CON, 0x8),
> > > + .pmic_rst_reg = MT6323_TOP_RST_MISC,
> > > +};
> > > +
> > > +struct mtk_pmic_keys_info {
> > > + struct mtk_pmic_keys *keys;
> > > + const struct mtk_pmic_keys_regs *regs;
> > > + unsigned int keycode;
> > > + int irq;
> > > + bool wakeup:1;
> > > +};
> > > +
> > > +struct mtk_pmic_keys {
> > > + struct input_dev *input_dev;
> > > + struct device *dev;
> > > + struct regmap *regmap;
> > > + struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
> > > +};
> > > +
> > > +enum mtk_pmic_keys_lp_mode {
> > > + LP_DISABLE,
> > > + LP_ONEKEY,
> > > + LP_TWOKEY,
> > > +};
> > > +
> > > +static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
> > > + u32 pmic_rst_reg)
> > > +{
> > > + int ret;
> > > + u32 long_press_mode, long_press_debounce;
> > > +
> > > + ret = of_property_read_u32(keys->dev->of_node,
> > > + "power-off-time-sec", &long_press_debounce);
> > > + if (ret)
> > > + long_press_debounce = 0;
> > > +
> > > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > + MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
> > > + long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
> > > +
> > > + ret = of_property_read_u32(keys->dev->of_node,
> > > + "mediatek,long-press-mode", &long_press_mode);
> > > + if (ret)
> > > + long_press_mode = LP_DISABLE;
> > > +
> > > + switch (long_press_mode) {
> > > + case LP_ONEKEY:
> > > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > + MTK_PMIC_PWRKEY_RST,
> > > + MTK_PMIC_PWRKEY_RST);
> > > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > + MTK_PMIC_HOMEKEY_RST,
> > > + 0);
> > > + break;
> > > + case LP_TWOKEY:
> > > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > + MTK_PMIC_PWRKEY_RST,
> > > + MTK_PMIC_PWRKEY_RST);
> > > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > + MTK_PMIC_HOMEKEY_RST,
> > > + MTK_PMIC_HOMEKEY_RST);
> > > + break;
> > > + case LP_DISABLE:
> > > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > + MTK_PMIC_PWRKEY_RST,
> > > + 0);
> > > + regmap_update_bits(keys->regmap, pmic_rst_reg,
> > > + MTK_PMIC_HOMEKEY_RST,
> > > + 0);
> > > + break;
> > > + default:
> > > + break;
> > > + }
> > > +}
> > > +
> > > +static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
> > > +{
> > > + struct mtk_pmic_keys_info *info = data;
> > > + u32 key_deb, pressed;
> > > +
> > > + regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
> > > +
> > > + key_deb &= info->regs->deb_mask;
> > > +
> > > + pressed = !key_deb;
> > > +
> > > + input_report_key(info->keys->input_dev, info->keycode, pressed);
> > > + input_sync(info->keys->input_dev);
> > > +
> > > + dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
> > > + pressed ? "pressed" : "released", info->keycode);
> > > +
> > > + return IRQ_HANDLED;
> > > +}
> > > +
> > > +static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
> > > + struct mtk_pmic_keys_info *info)
> > > +{
> > > + int ret;
> > > +
> > > + info->keys = keys;
> > > +
> > > + ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
> > > + info->regs->intsel_mask,
> > > + info->regs->intsel_mask);
> > > + if (ret < 0)
> > > + return ret;
> > > +
> > > + ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
> > > + mtk_pmic_keys_irq_handler_thread,
> > > + IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
> > > + "mtk-pmic-keys", info);
> > > + if (ret) {
> > > + dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
> > > + info->irq, ret);
> > > + return ret;
> > > + }
> > > +
> > > + input_set_capability(keys->input_dev, EV_KEY, info->keycode);
> > > +
> > > + return 0;
> > > +}
> > > +
> > > +#ifdef CONFIG_PM_SLEEP
> > > +static int mtk_pmic_keys_suspend(struct device *dev)
> >
> > Please use __maybe_unused annotation instead of #ifdef guard.
> >
> > > +{
> > > + struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > > + int index;
> > > +
> > > + for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > > + if (keys->keys[index].wakeup)
> > > + enable_irq_wake(keys->keys[index].irq);
> > > + }
> > > +
> > > + return 0;
> > > +}
> > > +
> > > +static int mtk_pmic_keys_resume(struct device *dev)
> >
> > __maybe_unused here as well.
> >
> > > +{
> > > + struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
> > > + int index;
> > > +
> > > + for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
> > > + if (keys->keys[index].wakeup)
> > > + disable_irq_wake(keys->keys[index].irq);
> > > + }
> > > +
> > > + return 0;
> > > +}
> > > +#endif
> > > +
> > > +static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
> > > + mtk_pmic_keys_resume);
> > > +
> > > +static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
> > > + {
> > > + .compatible = "mediatek,mt6397-keys",
> > > + .data = &mt6397_regs,
> > > + }, {
> > > + .compatible = "mediatek,mt6323-keys",
> > > + .data = &mt6323_regs,
> > > + }, {
> > > + /* sentinel */
> > > + }
> > > +};
> > > +MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
> > > +
> > > +static int mtk_pmic_keys_probe(struct platform_device *pdev)
> > > +{
> > > + int error, index = 0;
> > > + unsigned int keycount;
> > > + struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
> > > + struct device_node *node = pdev->dev.of_node, *child;
> > > + struct mtk_pmic_keys *keys;
> > > + const struct mtk_pmic_regs *mtk_pmic_regs;
> > > + struct input_dev *input_dev;
> > > + const struct of_device_id *of_id =
> > > + of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
> > > +
> > > + keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
> > > + if (!keys)
> > > + return -ENOMEM;
> > > +
> > > + keys->dev = &pdev->dev;
> > > + keys->regmap = pmic_chip->regmap;
> > > + mtk_pmic_regs = of_id->data;
> > > +
> > > + keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
> > > + if (!input_dev) {
> > > + dev_err(keys->dev, "input allocate device fail.\n");
> > > + return -ENOMEM;
> > > + }
> > > +
> > > + input_dev->name = "mtk-pmic-keys";
> > > + input_dev->id.bustype = BUS_HOST;
> > > + input_dev->id.vendor = 0x0001;
> > > + input_dev->id.product = 0x0001;
> > > + input_dev->id.version = 0x0001;
> > > +
> > > + keycount = device_get_child_node_count(keys->dev);
> >
> > Since you are using of_* API everywhere else I'd rather we used
> > of_get_available_child_count() here.
> >
> >
> > Once these 2 issues are fixed please feel free to add
> >
> > Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> >
> > I assume it will all go though MFD tree, right?
>
> I will fix these 2 issues and send the new version.
>
> Since patch 2,3,4,5 of this series are all for input driver, could you
> help to merge them with the new version later?
Even though they applicable to the input component they do touch MFD
domain... Lee, how do you want to proceed here so we do not hold up the
patch series for too long?
Thanks.
--
Dmitry
^ permalink raw reply
* [PATCH v6 4/6] dt-bindings: mfd: Add bindings for the keys as subnode of PMIC
From: Chen Zhong @ 2017-10-25 13:16 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
Linus Walleij, Beomho Seo, Javier Martinez Canillas, Jaechul Lee,
Chen Zhong, Krzysztof Kozlowski, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1508937364-30054-1-git-send-email-chen.zhong@mediatek.com>
This patch adds documentation for device tree bindings for keys support
as the subnode of MT6397/MT6323 PMIC.
Acked-by: Rob Herring <robh@kernel.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
Documentation/devicetree/bindings/mfd/mt6397.txt | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/Documentation/devicetree/bindings/mfd/mt6397.txt b/Documentation/devicetree/bindings/mfd/mt6397.txt
index 522a3bb..d1df77f 100644
--- a/Documentation/devicetree/bindings/mfd/mt6397.txt
+++ b/Documentation/devicetree/bindings/mfd/mt6397.txt
@@ -7,6 +7,7 @@ MT6397/MT6323 is a multifunction device with the following sub modules:
- GPIO
- Clock
- LED
+- Keys
It is interfaced to host controller using SPI interface by a proprietary hardware
called PMIC wrapper or pwrap. MT6397/MT6323 MFD is a child device of pwrap.
@@ -40,6 +41,11 @@ Optional subnodes:
- compatible: "mediatek,mt6323-led"
see Documentation/devicetree/bindings/leds/leds-mt6323.txt
+- keys
+ Required properties:
+ - compatible: "mediatek,mt6397-keys" or "mediatek,mt6323-keys"
+ see Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
+
Example:
pwrap: pwrap@1000f000 {
compatible = "mediatek,mt8135-pwrap";
--
1.7.9.5
^ permalink raw reply related
* [PATCH v6 5/6] input: Add MediaTek PMIC keys support
From: Chen Zhong @ 2017-10-25 13:16 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
Linus Walleij, Beomho Seo, Javier Martinez Canillas, Jaechul Lee,
Chen Zhong, Krzysztof Kozlowski, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1508937364-30054-1-git-send-email-chen.zhong@mediatek.com>
This patch add support to handle MediaTek PMIC MT6397/MT6323 key
interrupts including pwrkey and homekey, also add setting for
long press key shutdown behavior.
Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
drivers/input/keyboard/Kconfig | 9 +
drivers/input/keyboard/Makefile | 1 +
drivers/input/keyboard/mtk-pmic-keys.c | 339 ++++++++++++++++++++++++++++++++
3 files changed, 349 insertions(+)
create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index 4c4ab1c..bd4e20a 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -756,4 +756,13 @@ config KEYBOARD_BCM
To compile this driver as a module, choose M here: the
module will be called bcm-keypad.
+config KEYBOARD_MTK_PMIC
+ tristate "MediaTek PMIC keys support"
+ depends on MFD_MT6397
+ help
+ Say Y here if you want to use the pmic keys (powerkey/homekey).
+
+ To compile this driver as a module, choose M here: the
+ module will be called pmic-keys.
+
endif
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index d2338ba..20c0b98 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_KEYBOARD_MATRIX) += matrix_keypad.o
obj-$(CONFIG_KEYBOARD_MAX7359) += max7359_keypad.o
obj-$(CONFIG_KEYBOARD_MCS) += mcs_touchkey.o
obj-$(CONFIG_KEYBOARD_MPR121) += mpr121_touchkey.o
+obj-$(CONFIG_KEYBOARD_MTK_PMIC) += mtk-pmic-keys.o
obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
obj-$(CONFIG_KEYBOARD_NOMADIK) += nomadik-ske-keypad.o
obj-$(CONFIG_KEYBOARD_NSPIRE) += nspire-keypad.o
diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c
new file mode 100644
index 0000000..02c67a1
--- /dev/null
+++ b/drivers/input/keyboard/mtk-pmic-keys.c
@@ -0,0 +1,339 @@
+/*
+ * Copyright (C) 2017 MediaTek, Inc.
+ *
+ * Author: Chen Zhong <chen.zhong@mediatek.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * 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/module.h>
+#include <linux/kernel.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/regmap.h>
+#include <linux/mfd/mt6323/registers.h>
+#include <linux/mfd/mt6397/registers.h>
+#include <linux/mfd/mt6397/core.h>
+
+#define MTK_PMIC_PWRKEY_RST_EN_MASK 0x1
+#define MTK_PMIC_PWRKEY_RST_EN_SHIFT 6
+#define MTK_PMIC_HOMEKEY_RST_EN_MASK 0x1
+#define MTK_PMIC_HOMEKEY_RST_EN_SHIFT 5
+#define MTK_PMIC_RST_DU_MASK 0x3
+#define MTK_PMIC_RST_DU_SHIFT 8
+
+#define MTK_PMIC_PWRKEY_RST \
+ (MTK_PMIC_PWRKEY_RST_EN_MASK << MTK_PMIC_PWRKEY_RST_EN_SHIFT)
+#define MTK_PMIC_HOMEKEY_RST \
+ (MTK_PMIC_HOMEKEY_RST_EN_MASK << MTK_PMIC_HOMEKEY_RST_EN_SHIFT)
+
+#define MTK_PMIC_PWRKEY_INDEX 0
+#define MTK_PMIC_HOMEKEY_INDEX 1
+#define MTK_PMIC_MAX_KEY_COUNT 2
+
+struct mtk_pmic_keys_regs {
+ u32 deb_reg;
+ u32 deb_mask;
+ u32 intsel_reg;
+ u32 intsel_mask;
+};
+
+#define MTK_PMIC_KEYS_REGS(_deb_reg, _deb_mask, \
+ _intsel_reg, _intsel_mask) \
+{ \
+ .deb_reg = _deb_reg, \
+ .deb_mask = _deb_mask, \
+ .intsel_reg = _intsel_reg, \
+ .intsel_mask = _intsel_mask, \
+}
+
+struct mtk_pmic_regs {
+ const struct mtk_pmic_keys_regs keys_regs[MTK_PMIC_MAX_KEY_COUNT];
+ u32 pmic_rst_reg;
+};
+
+static const struct mtk_pmic_regs mt6397_regs = {
+ .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
+ MTK_PMIC_KEYS_REGS(MT6397_CHRSTATUS,
+ 0x8, MT6397_INT_RSV, 0x10),
+ .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
+ MTK_PMIC_KEYS_REGS(MT6397_OCSTATUS2,
+ 0x10, MT6397_INT_RSV, 0x8),
+ .pmic_rst_reg = MT6397_TOP_RST_MISC,
+};
+
+static const struct mtk_pmic_regs mt6323_regs = {
+ .keys_regs[MTK_PMIC_PWRKEY_INDEX] =
+ MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
+ 0x2, MT6323_INT_MISC_CON, 0x10),
+ .keys_regs[MTK_PMIC_HOMEKEY_INDEX] =
+ MTK_PMIC_KEYS_REGS(MT6323_CHRSTATUS,
+ 0x4, MT6323_INT_MISC_CON, 0x8),
+ .pmic_rst_reg = MT6323_TOP_RST_MISC,
+};
+
+struct mtk_pmic_keys_info {
+ struct mtk_pmic_keys *keys;
+ const struct mtk_pmic_keys_regs *regs;
+ unsigned int keycode;
+ int irq;
+ bool wakeup:1;
+};
+
+struct mtk_pmic_keys {
+ struct input_dev *input_dev;
+ struct device *dev;
+ struct regmap *regmap;
+ struct mtk_pmic_keys_info keys[MTK_PMIC_MAX_KEY_COUNT];
+};
+
+enum mtk_pmic_keys_lp_mode {
+ LP_DISABLE,
+ LP_ONEKEY,
+ LP_TWOKEY,
+};
+
+static void mtk_pmic_keys_lp_reset_setup(struct mtk_pmic_keys *keys,
+ u32 pmic_rst_reg)
+{
+ int ret;
+ u32 long_press_mode, long_press_debounce;
+
+ ret = of_property_read_u32(keys->dev->of_node,
+ "power-off-time-sec", &long_press_debounce);
+ if (ret)
+ long_press_debounce = 0;
+
+ regmap_update_bits(keys->regmap, pmic_rst_reg,
+ MTK_PMIC_RST_DU_MASK << MTK_PMIC_RST_DU_SHIFT,
+ long_press_debounce << MTK_PMIC_RST_DU_SHIFT);
+
+ ret = of_property_read_u32(keys->dev->of_node,
+ "mediatek,long-press-mode", &long_press_mode);
+ if (ret)
+ long_press_mode = LP_DISABLE;
+
+ switch (long_press_mode) {
+ case LP_ONEKEY:
+ regmap_update_bits(keys->regmap, pmic_rst_reg,
+ MTK_PMIC_PWRKEY_RST,
+ MTK_PMIC_PWRKEY_RST);
+ regmap_update_bits(keys->regmap, pmic_rst_reg,
+ MTK_PMIC_HOMEKEY_RST,
+ 0);
+ break;
+ case LP_TWOKEY:
+ regmap_update_bits(keys->regmap, pmic_rst_reg,
+ MTK_PMIC_PWRKEY_RST,
+ MTK_PMIC_PWRKEY_RST);
+ regmap_update_bits(keys->regmap, pmic_rst_reg,
+ MTK_PMIC_HOMEKEY_RST,
+ MTK_PMIC_HOMEKEY_RST);
+ break;
+ case LP_DISABLE:
+ regmap_update_bits(keys->regmap, pmic_rst_reg,
+ MTK_PMIC_PWRKEY_RST,
+ 0);
+ regmap_update_bits(keys->regmap, pmic_rst_reg,
+ MTK_PMIC_HOMEKEY_RST,
+ 0);
+ break;
+ default:
+ break;
+ }
+}
+
+static irqreturn_t mtk_pmic_keys_irq_handler_thread(int irq, void *data)
+{
+ struct mtk_pmic_keys_info *info = data;
+ u32 key_deb, pressed;
+
+ regmap_read(info->keys->regmap, info->regs->deb_reg, &key_deb);
+
+ key_deb &= info->regs->deb_mask;
+
+ pressed = !key_deb;
+
+ input_report_key(info->keys->input_dev, info->keycode, pressed);
+ input_sync(info->keys->input_dev);
+
+ dev_dbg(info->keys->dev, "(%s) key =%d using PMIC\n",
+ pressed ? "pressed" : "released", info->keycode);
+
+ return IRQ_HANDLED;
+}
+
+static int mtk_pmic_key_setup(struct mtk_pmic_keys *keys,
+ struct mtk_pmic_keys_info *info)
+{
+ int ret;
+
+ info->keys = keys;
+
+ ret = regmap_update_bits(keys->regmap, info->regs->intsel_reg,
+ info->regs->intsel_mask,
+ info->regs->intsel_mask);
+ if (ret < 0)
+ return ret;
+
+ ret = devm_request_threaded_irq(keys->dev, info->irq, NULL,
+ mtk_pmic_keys_irq_handler_thread,
+ IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
+ "mtk-pmic-keys", info);
+ if (ret) {
+ dev_err(keys->dev, "Failed to request IRQ: %d: %d\n",
+ info->irq, ret);
+ return ret;
+ }
+
+ input_set_capability(keys->input_dev, EV_KEY, info->keycode);
+
+ return 0;
+}
+
+static int __maybe_unused mtk_pmic_keys_suspend(struct device *dev)
+{
+ struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
+ int index;
+
+ for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
+ if (keys->keys[index].wakeup)
+ enable_irq_wake(keys->keys[index].irq);
+ }
+
+ return 0;
+}
+
+static int __maybe_unused mtk_pmic_keys_resume(struct device *dev)
+{
+ struct mtk_pmic_keys *keys = dev_get_drvdata(dev);
+ int index;
+
+ for (index = 0; index < MTK_PMIC_MAX_KEY_COUNT; index++) {
+ if (keys->keys[index].wakeup)
+ disable_irq_wake(keys->keys[index].irq);
+ }
+
+ return 0;
+}
+
+static SIMPLE_DEV_PM_OPS(mtk_pmic_keys_pm_ops, mtk_pmic_keys_suspend,
+ mtk_pmic_keys_resume);
+
+static const struct of_device_id of_mtk_pmic_keys_match_tbl[] = {
+ {
+ .compatible = "mediatek,mt6397-keys",
+ .data = &mt6397_regs,
+ }, {
+ .compatible = "mediatek,mt6323-keys",
+ .data = &mt6323_regs,
+ }, {
+ /* sentinel */
+ }
+};
+MODULE_DEVICE_TABLE(of, of_mtk_pmic_keys_match_tbl);
+
+static int mtk_pmic_keys_probe(struct platform_device *pdev)
+{
+ int error, index = 0;
+ unsigned int keycount;
+ struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent);
+ struct device_node *node = pdev->dev.of_node, *child;
+ struct mtk_pmic_keys *keys;
+ const struct mtk_pmic_regs *mtk_pmic_regs;
+ struct input_dev *input_dev;
+ const struct of_device_id *of_id =
+ of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
+
+ keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
+ if (!keys)
+ return -ENOMEM;
+
+ keys->dev = &pdev->dev;
+ keys->regmap = pmic_chip->regmap;
+ mtk_pmic_regs = of_id->data;
+
+ keys->input_dev = input_dev = devm_input_allocate_device(keys->dev);
+ if (!input_dev) {
+ dev_err(keys->dev, "input allocate device fail.\n");
+ return -ENOMEM;
+ }
+
+ input_dev->name = "mtk-pmic-keys";
+ input_dev->id.bustype = BUS_HOST;
+ input_dev->id.vendor = 0x0001;
+ input_dev->id.product = 0x0001;
+ input_dev->id.version = 0x0001;
+
+ keycount = of_get_available_child_count(node);
+ if (keycount > MTK_PMIC_MAX_KEY_COUNT) {
+ dev_err(keys->dev, "too many keys defined (%d)\n", keycount);
+ return -EINVAL;
+ }
+
+ for_each_child_of_node(node, child) {
+ keys->keys[index].regs = &mtk_pmic_regs->keys_regs[index];
+
+ keys->keys[index].irq = platform_get_irq(pdev, index);
+ if (keys->keys[index].irq < 0)
+ return keys->keys[index].irq;
+
+ error = of_property_read_u32(child,
+ "linux,keycodes", &keys->keys[index].keycode);
+ if (error) {
+ dev_err(keys->dev,
+ "failed to read key:%d linux,keycode property: %d\n",
+ index, error);
+ return error;
+ }
+
+ if (of_property_read_bool(child, "wakeup-source"))
+ keys->keys[index].wakeup = true;
+
+ error = mtk_pmic_key_setup(keys, &keys->keys[index]);
+ if (error)
+ return error;
+
+ index++;
+ }
+
+ error = input_register_device(input_dev);
+ if (error) {
+ dev_err(&pdev->dev,
+ "register input device failed (%d)\n", error);
+ return error;
+ }
+
+ mtk_pmic_keys_lp_reset_setup(keys, mtk_pmic_regs->pmic_rst_reg);
+
+ platform_set_drvdata(pdev, keys);
+
+ return 0;
+}
+
+static struct platform_driver pmic_keys_pdrv = {
+ .probe = mtk_pmic_keys_probe,
+ .driver = {
+ .name = "mtk-pmic-keys",
+ .of_match_table = of_mtk_pmic_keys_match_tbl,
+ .pm = &mtk_pmic_keys_pm_ops,
+ },
+};
+
+module_platform_driver(pmic_keys_pdrv);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
+MODULE_DESCRIPTION("MTK pmic-keys driver v0.1");
--
1.7.9.5
^ permalink raw reply related
* [PATCH v6 3/6] dt-bindings: input: Add document bindings for mtk-pmic-keys
From: Chen Zhong @ 2017-10-25 13:16 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
Linus Walleij, Beomho Seo, Javier Martinez Canillas, Jaechul Lee,
Chen Zhong, Krzysztof Kozlowski, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1508937364-30054-1-git-send-email-chen.zhong@mediatek.com>
This patch adds the device tree binding documentation for the MediaTek
pmic keys found on PMIC MT6397/MT6323.
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
.../devicetree/bindings/input/mtk-pmic-keys.txt | 43 ++++++++++++++++++++
1 file changed, 43 insertions(+)
create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
diff --git a/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt b/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
new file mode 100644
index 0000000..2888d07
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
@@ -0,0 +1,43 @@
+MediaTek MT6397/MT6323 PMIC Keys Device Driver
+
+There are two key functions provided by MT6397/MT6323 PMIC, pwrkey
+and homekey. The key functions are defined as the subnode of the function
+node provided by MT6397/MT6323 PMIC that is being defined as one kind
+of Muti-Function Device (MFD)
+
+For MT6397/MT6323 MFD bindings see:
+Documentation/devicetree/bindings/mfd/mt6397.txt
+
+Required properties:
+- compatible: "mediatek,mt6397-keys" or "mediatek,mt6323-keys"
+- linux,keycodes: See Documentation/devicetree/bindings/input/keys.txt
+
+Optional Properties:
+- wakeup-source: See Documentation/devicetree/bindings/power/wakeup-source.txt
+- mediatek,long-press-mode: Long press key shutdown setting, 1 for
+ pwrkey only, 2 for pwrkey/homekey together, others for disabled.
+- power-off-time-sec: See Documentation/devicetree/bindings/input/keys.txt
+
+Example:
+
+ pmic: mt6397 {
+ compatible = "mediatek,mt6397";
+
+ ...
+
+ mt6397keys: mt6397keys {
+ compatible = "mediatek,mt6397-keys";
+ mediatek,long-press-mode = <1>;
+ power-off-time-sec = <0>;
+
+ power {
+ linux,keycodes = <116>;
+ wakeup-source;
+ };
+
+ home {
+ linux,keycodes = <114>;
+ };
+ };
+
+ };
--
1.7.9.5
^ permalink raw reply related
* [PATCH v6 6/6] mfd: mt6397: Add PMIC keys support to MT6397 driver
From: Chen Zhong @ 2017-10-25 13:16 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
Linus Walleij, Beomho Seo, Javier Martinez Canillas, Jaechul Lee,
Chen Zhong, Krzysztof Kozlowski, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1508937364-30054-1-git-send-email-chen.zhong@mediatek.com>
This patch adds compatible strings and interrupts for pmic keys
which serves as child device of MFD.
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
drivers/mfd/mt6397-core.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/drivers/mfd/mt6397-core.c b/drivers/mfd/mt6397-core.c
index 6546d7f..77b64bd 100644
--- a/drivers/mfd/mt6397-core.c
+++ b/drivers/mfd/mt6397-core.c
@@ -43,6 +43,16 @@
},
};
+static const struct resource mt6323_keys_resources[] = {
+ DEFINE_RES_IRQ(MT6323_IRQ_STATUS_PWRKEY),
+ DEFINE_RES_IRQ(MT6323_IRQ_STATUS_FCHRKEY),
+};
+
+static const struct resource mt6397_keys_resources[] = {
+ DEFINE_RES_IRQ(MT6397_IRQ_PWRKEY),
+ DEFINE_RES_IRQ(MT6397_IRQ_HOMEKEY),
+};
+
static const struct mfd_cell mt6323_devs[] = {
{
.name = "mt6323-regulator",
@@ -50,6 +60,11 @@
}, {
.name = "mt6323-led",
.of_compatible = "mediatek,mt6323-led"
+ }, {
+ .name = "mtk-pmic-keys",
+ .num_resources = ARRAY_SIZE(mt6323_keys_resources),
+ .resources = mt6323_keys_resources,
+ .of_compatible = "mediatek,mt6323-keys"
},
};
@@ -71,7 +86,12 @@
}, {
.name = "mt6397-pinctrl",
.of_compatible = "mediatek,mt6397-pinctrl",
- },
+ }, {
+ .name = "mtk-pmic-keys",
+ .num_resources = ARRAY_SIZE(mt6397_keys_resources),
+ .resources = mt6397_keys_resources,
+ .of_compatible = "mediatek,mt6397-keys"
+ }
};
static void mt6397_irq_lock(struct irq_data *data)
--
1.7.9.5
^ permalink raw reply related
* [PATCH v6 1/6] mfd: mt6397: create irq mappings in mfd core driver
From: Chen Zhong @ 2017-10-25 13:15 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
Linus Walleij, Beomho Seo, Javier Martinez Canillas, Jaechul Lee,
Chen Zhong, Krzysztof Kozlowski, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1508937364-30054-1-git-send-email-chen.zhong@mediatek.com>
The core driver should create and manage irq mappings instead of
leaf drivers. This patch change to pass irq domain to
devm_mfd_add_devices() and it will create mapping for irq resources
automatically. And remove irq mapping in rtc driver since this has
been done in core driver.
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
Acked-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
drivers/mfd/mt6397-core.c | 4 ++--
drivers/rtc/rtc-mt6397.c | 7 +++----
2 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/drivers/mfd/mt6397-core.c b/drivers/mfd/mt6397-core.c
index 04a601f..6546d7f 100644
--- a/drivers/mfd/mt6397-core.c
+++ b/drivers/mfd/mt6397-core.c
@@ -289,7 +289,7 @@ static int mt6397_probe(struct platform_device *pdev)
ret = devm_mfd_add_devices(&pdev->dev, -1, mt6323_devs,
ARRAY_SIZE(mt6323_devs), NULL,
- 0, NULL);
+ 0, pmic->irq_domain);
break;
case MT6397_CID_CODE:
@@ -304,7 +304,7 @@ static int mt6397_probe(struct platform_device *pdev)
ret = devm_mfd_add_devices(&pdev->dev, -1, mt6397_devs,
ARRAY_SIZE(mt6397_devs), NULL,
- 0, NULL);
+ 0, pmic->irq_domain);
break;
default:
diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c
index 1a61fa5..385f830 100644
--- a/drivers/rtc/rtc-mt6397.c
+++ b/drivers/rtc/rtc-mt6397.c
@@ -322,10 +322,9 @@ static int mtk_rtc_probe(struct platform_device *pdev)
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
rtc->addr_base = res->start;
- res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
- rtc->irq = irq_create_mapping(mt6397_chip->irq_domain, res->start);
- if (rtc->irq <= 0)
- return -EINVAL;
+ rtc->irq = platform_get_irq(pdev, 0);
+ if (rtc->irq < 0)
+ return rtc->irq;
rtc->regmap = mt6397_chip->regmap;
rtc->dev = &pdev->dev;
--
1.7.9.5
^ permalink raw reply related
* [PATCH v6 2/6] dt-bindings: input: Add common keyboard document bindings
From: Chen Zhong @ 2017-10-25 13:16 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
Linus Walleij, Beomho Seo, Javier Martinez Canillas, Jaechul Lee,
Chen Zhong, Krzysztof Kozlowski, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
In-Reply-To: <1508937364-30054-1-git-send-email-chen.zhong@mediatek.com>
This patch adds the device tree binding documentation for common
keyboard.
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Chen Zhong <chen.zhong@mediatek.com>
---
Documentation/devicetree/bindings/input/keys.txt | 8 ++++++++
1 file changed, 8 insertions(+)
create mode 100644 Documentation/devicetree/bindings/input/keys.txt
diff --git a/Documentation/devicetree/bindings/input/keys.txt b/Documentation/devicetree/bindings/input/keys.txt
new file mode 100644
index 0000000..a0ea7eb
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/keys.txt
@@ -0,0 +1,8 @@
+General Keys Properties:
+
+Optional properties for Keys:
+- power-off-time-sec: Duration in seconds which the key should be kept
+ pressed for device to power off automatically. Device with key pressed
+ shutdown feature can specify this property.
+- linux,keycodes: Specifies the numeric keycode values to be used for
+ reporting key presses.
\ No newline at end of file
--
1.7.9.5
^ permalink raw reply related
* [PATCH v6 0/6] Add MediaTek PMIC keys support
From: Chen Zhong @ 2017-10-25 13:15 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Lee Jones, Alexandre Belloni
Cc: Mark Rutland, Matthias Brugger, Eddie Huang, Alessandro Zummo,
Linus Walleij, Beomho Seo, Javier Martinez Canillas, Jaechul Lee,
Chen Zhong, Krzysztof Kozlowski, linux-input, devicetree,
linux-kernel, linux-arm-kernel, linux-mediatek, linux-rtc
MediaTek PMIC are multi-function devices that can handle key interrupts, typically there are two keys attached to PMIC, which called pwrkey and homekey. PWRKEY usually used to wake up system from sleep. Homekey can used as volume down key due to board design. Long press keys can shutdown PMIC, the mode can be choose to be one key only or two keys together.
This series add support for key functions for MediaTek PMIC MT6397/MT6323.
Changes since v5:
- use __maybe_unused annotation instead of #ifdef guard
- use of_* API instead of device_* API
Changes since v4:
- rebase to Linux 4.14-rc1
- add a common keyboard binding document
- use child device tree node to define each key
Changes since v3:
- make the naming to be consistent as mtk_pmic or MTK_PMIC
- add suspend/resume functions to enable/disable irq
- change binding properties to define wakeup sources
Changes since v2:
- use standard properties for keycodes and debounce time
- change to use platform_get_irq in leaf drivers
- use better ways to define IRQ resources
Changes since v1:
- create irq mappings in mfd core driver instead of leaf drivers
- remove some unused parts in mtk-pmic-keys driver
Chen Zhong (6):
mfd: mt6397: create irq mappings in mfd core driver
dt-bindings: input: Add common keyboard document bindings
dt-bindings: input: Add document bindings for mtk-pmic-keys
dt-bindings: mfd: Add bindings for the keys as subnode of PMIC
input: Add MediaTek PMIC keys support
mfd: mt6397: Add PMIC keys support to MT6397 driver
Documentation/devicetree/bindings/input/keys.txt | 8 +
.../devicetree/bindings/input/mtk-pmic-keys.txt | 43 +++
Documentation/devicetree/bindings/mfd/mt6397.txt | 6 +
drivers/input/keyboard/Kconfig | 9 +
drivers/input/keyboard/Makefile | 1 +
drivers/input/keyboard/mtk-pmic-keys.c | 339 +++++++++++++++++++++
drivers/mfd/mt6397-core.c | 26 +-
drivers/rtc/rtc-mt6397.c | 7 +-
8 files changed, 432 insertions(+), 7 deletions(-)
create mode 100644 Documentation/devicetree/bindings/input/keys.txt
create mode 100644 Documentation/devicetree/bindings/input/mtk-pmic-keys.txt
create mode 100644 drivers/input/keyboard/mtk-pmic-keys.c
--
1.9.1
^ permalink raw reply
page: next (older) | prev (newer) | latest
- recent:[subjects (threaded)|topics (new)|topics (active)]
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox