From: Lee Jones <lee.jones@linaro.org>
To: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Chen-Yu Tsai <wens@csie.org>,
Alessandro Zummo <a.zummo@towertech.it>,
Maxime Ripard <maxime.ripard@free-electrons.com>,
linux-kernel@vger.kernel.org, rtc-linux@googlegroups.com,
devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org
Subject: [rtc-linux] Re: [PATCH v5 3/7] rtc: ac100: Add RTC driver for X-Powers AC100
Date: Mon, 8 Aug 2016 12:57:07 +0100 [thread overview]
Message-ID: <20160808115707.GP5243@dell> (raw)
In-Reply-To: <20160708162152.GM22202@piout.net>
On Fri, 08 Jul 2016, Alexandre Belloni wrote:
> On 08/07/2016 at 22:33:38 +0800, Chen-Yu Tsai wrote :
> > X-Powers AC100 is a codec / RTC combo chip. This driver supports
> > the RTC sub-device.
> >=20
> > The RTC block also has clock outputs and non-volatile storage.
> > Non-volatile storage wthin the RTC hardware is not supported.
> > Clock output support is added in the next patch.
> >=20
> > Signed-off-by: Chen-Yu Tsai <wens@csie.org>
> Acked-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Applied, thanks.
> > ---
> > Changes since v4:
> >=20
> > - None.
> >=20
> > Changes since v3:
> >=20
> > - Moved month/year conversion offsets inline with reg[] assignments
> > - Used platform_get_irq instead of of_get_irq
> > - Dropped separate mutex and use rtc->ops_lock in interrupt handler
> > - Dropped of_irq.h from includes
> > - Added of_device_id and .of_match_table
> >=20
> > Changes since v2:
> >=20
> > - Fixed off-by-1 errors in register mask macros
> > - Fixed copy-paste error in set_alarm function
> > - Drop uie_unsupported, since the alarm works now
> > - Fixed up copyright notice
> > - Changed license to GPL v2, matching mfd driver
> >=20
> > ---
> > drivers/rtc/Kconfig | 10 ++
> > drivers/rtc/Makefile | 1 +
> > drivers/rtc/rtc-ac100.c | 325 ++++++++++++++++++++++++++++++++++++++++=
++++++++
> > 3 files changed, 336 insertions(+)
> > create mode 100644 drivers/rtc/rtc-ac100.c
> >=20
> > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > index 18639e0cb6e2..b9d7cbb6bd76 100644
> > --- a/drivers/rtc/Kconfig
> > +++ b/drivers/rtc/Kconfig
> > @@ -183,6 +183,16 @@ config RTC_DRV_ABX80X
> > This driver can also be built as a module. If so, the module
> > will be called rtc-abx80x.
> > =20
> > +config RTC_DRV_AC100
> > + tristate "X-Powers AC100"
> > + depends on MFD_AC100
> > + help
> > + If you say yes here you get support for the real-time clock found
> > + in X-Powers AC100 family peripheral ICs.
> > +
> > + This driver can also be built as a module. If so, the module
> > + will be called rtc-ac100.
> > +
> > config RTC_DRV_AS3722
> > tristate "ams AS3722 RTC driver"
> > depends on MFD_AS3722
> > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> > index ea2833723fa9..b07c28779573 100644
> > --- a/drivers/rtc/Makefile
> > +++ b/drivers/rtc/Makefile
> > @@ -26,6 +26,7 @@ obj-$(CONFIG_RTC_DRV_AB3100) +=3D rtc-ab3100.o
> > obj-$(CONFIG_RTC_DRV_AB8500) +=3D rtc-ab8500.o
> > obj-$(CONFIG_RTC_DRV_ABB5ZES3) +=3D rtc-ab-b5ze-s3.o
> > obj-$(CONFIG_RTC_DRV_ABX80X) +=3D rtc-abx80x.o
> > +obj-$(CONFIG_RTC_DRV_AC100) +=3D rtc-ac100.o
> > obj-$(CONFIG_RTC_DRV_ARMADA38X) +=3D rtc-armada38x.o
> > obj-$(CONFIG_RTC_DRV_AS3722) +=3D rtc-as3722.o
> > obj-$(CONFIG_RTC_DRV_ASM9260) +=3D rtc-asm9260.o
> > diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c
> > new file mode 100644
> > index 000000000000..5a9ca89d04c7
> > --- /dev/null
> > +++ b/drivers/rtc/rtc-ac100.c
> > @@ -0,0 +1,325 @@
> > +/*
> > + * RTC Driver for X-Powers AC100
> > + *
> > + * Copyright (c) 2016 Chen-Yu Tsai
> > + *
> > + * Chen-Yu Tsai <wens@csie.org>
> > + *
> > + * This program is free software; you can redistribute it and/or modif=
y
> > + * it under the terms of the GNU General Public License version 2 as
> > + * published by the Free Software Foundation.
> > + *
> > + * 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 Licen=
se for
> > + * more details.
> > + */
> > +
> > +#include <linux/bcd.h>
> > +#include <linux/device.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/kernel.h>
> > +#include <linux/mfd/ac100.h>
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/of.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/regmap.h>
> > +#include <linux/rtc.h>
> > +#include <linux/types.h>
> > +
> > +/* Control register */
> > +#define AC100_RTC_CTRL_24HOUR BIT(0)
> > +
> > +/* RTC */
> > +#define AC100_RTC_SEC_MASK GENMASK(6, 0)
> > +#define AC100_RTC_MIN_MASK GENMASK(6, 0)
> > +#define AC100_RTC_HOU_MASK GENMASK(5, 0)
> > +#define AC100_RTC_WEE_MASK GENMASK(2, 0)
> > +#define AC100_RTC_DAY_MASK GENMASK(5, 0)
> > +#define AC100_RTC_MON_MASK GENMASK(4, 0)
> > +#define AC100_RTC_YEA_MASK GENMASK(7, 0)
> > +#define AC100_RTC_YEA_LEAP BIT(15)
> > +#define AC100_RTC_UPD_TRIGGER BIT(15)
> > +
> > +/* Alarm (wall clock) */
> > +#define AC100_ALM_INT_ENABLE BIT(0)
> > +
> > +#define AC100_ALM_SEC_MASK GENMASK(6, 0)
> > +#define AC100_ALM_MIN_MASK GENMASK(6, 0)
> > +#define AC100_ALM_HOU_MASK GENMASK(5, 0)
> > +#define AC100_ALM_WEE_MASK GENMASK(2, 0)
> > +#define AC100_ALM_DAY_MASK GENMASK(5, 0)
> > +#define AC100_ALM_MON_MASK GENMASK(4, 0)
> > +#define AC100_ALM_YEA_MASK GENMASK(7, 0)
> > +#define AC100_ALM_ENABLE_FLAG BIT(15)
> > +#define AC100_ALM_UPD_TRIGGER BIT(15)
> > +
> > +/*
> > + * The year parameter passed to the driver is usually an offset relati=
ve to
> > + * the year 1900. This macro is used to convert this offset to another=
one
> > + * relative to the minimum year allowed by the hardware.
> > + *
> > + * The year range is 1970 - 2069. This range is selected to match Allw=
inner's
> > + * driver.
> > + */
> > +#define AC100_YEAR_MIN 1970
> > +#define AC100_YEAR_MAX 2069
> > +#define AC100_YEAR_OFF (AC100_YEAR_MIN - 1900)
> > +
> > +struct ac100_rtc_dev {
> > + struct rtc_device *rtc;
> > + struct device *dev;
> > + struct regmap *regmap;
> > + int irq;
> > + unsigned long alarm;
> > +};
> > +
> > +static int ac100_rtc_get_time(struct device *dev, struct rtc_time *rtc=
_tm)
> > +{
> > + struct ac100_rtc_dev *chip =3D dev_get_drvdata(dev);
> > + struct regmap *regmap =3D chip->regmap;
> > + u16 reg[7];
> > + int ret;
> > +
> > + ret =3D regmap_bulk_read(regmap, AC100_RTC_SEC, reg, 7);
> > + if (ret)
> > + return ret;
> > +
> > + rtc_tm->tm_sec =3D bcd2bin(reg[0] & AC100_RTC_SEC_MASK);
> > + rtc_tm->tm_min =3D bcd2bin(reg[1] & AC100_RTC_MIN_MASK);
> > + rtc_tm->tm_hour =3D bcd2bin(reg[2] & AC100_RTC_HOU_MASK);
> > + rtc_tm->tm_wday =3D bcd2bin(reg[3] & AC100_RTC_WEE_MASK);
> > + rtc_tm->tm_mday =3D bcd2bin(reg[4] & AC100_RTC_DAY_MASK);
> > + rtc_tm->tm_mon =3D bcd2bin(reg[5] & AC100_RTC_MON_MASK) - 1;
> > + rtc_tm->tm_year =3D bcd2bin(reg[6] & AC100_RTC_YEA_MASK) +
> > + AC100_YEAR_OFF;
> > +
> > + return rtc_valid_tm(rtc_tm);
> > +}
> > +
> > +static int ac100_rtc_set_time(struct device *dev, struct rtc_time *rtc=
_tm)
> > +{
> > + struct ac100_rtc_dev *chip =3D dev_get_drvdata(dev);
> > + struct regmap *regmap =3D chip->regmap;
> > + int year;
> > + u16 reg[8];
> > +
> > + /* our RTC has a limited year range... */
> > + year =3D rtc_tm->tm_year - AC100_YEAR_OFF;
> > + if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
> > + dev_err(dev, "rtc only supports year in range %d - %d\n",
> > + AC100_YEAR_MIN, AC100_YEAR_MAX);
> > + return -EINVAL;
> > + }
> > +
> > + /* convert to BCD */
> > + reg[0] =3D bin2bcd(rtc_tm->tm_sec) & AC100_RTC_SEC_MASK;
> > + reg[1] =3D bin2bcd(rtc_tm->tm_min) & AC100_RTC_MIN_MASK;
> > + reg[2] =3D bin2bcd(rtc_tm->tm_hour) & AC100_RTC_HOU_MASK;
> > + reg[3] =3D bin2bcd(rtc_tm->tm_wday) & AC100_RTC_WEE_MASK;
> > + reg[4] =3D bin2bcd(rtc_tm->tm_mday) & AC100_RTC_DAY_MASK;
> > + reg[5] =3D bin2bcd(rtc_tm->tm_mon + 1) & AC100_RTC_MON_MASK;
> > + reg[6] =3D bin2bcd(year) & AC100_RTC_YEA_MASK;
> > + /* trigger write */
> > + reg[7] =3D AC100_RTC_UPD_TRIGGER;
> > +
> > + /* Is it a leap year? */
> > + if (is_leap_year(year + AC100_YEAR_OFF + 1900))
> > + reg[6] |=3D AC100_RTC_YEA_LEAP;
> > +
> > + return regmap_bulk_write(regmap, AC100_RTC_SEC, reg, 8);
> > +}
> > +
> > +static int ac100_rtc_alarm_irq_enable(struct device *dev, unsigned int=
en)
> > +{
> > + struct ac100_rtc_dev *chip =3D dev_get_drvdata(dev);
> > + struct regmap *regmap =3D chip->regmap;
> > + unsigned int val;
> > +
> > + val =3D en ? AC100_ALM_INT_ENABLE : 0;
> > +
> > + return regmap_write(regmap, AC100_ALM_INT_ENA, val);
> > +}
> > +
> > +static int ac100_rtc_get_alarm(struct device *dev, struct rtc_wkalrm *=
alrm)
> > +{
> > + struct ac100_rtc_dev *chip =3D dev_get_drvdata(dev);
> > + struct regmap *regmap =3D chip->regmap;
> > + struct rtc_time *alrm_tm =3D &alrm->time;
> > + u16 reg[7];
> > + unsigned int val;
> > + int ret;
> > +
> > + ret =3D regmap_read(regmap, AC100_ALM_INT_ENA, &val);
> > + if (ret)
> > + return ret;
> > +
> > + alrm->enabled =3D !!(val & AC100_ALM_INT_ENABLE);
> > +
> > + ret =3D regmap_bulk_read(regmap, AC100_ALM_SEC, reg, 7);
> > + if (ret)
> > + return ret;
> > +
> > + alrm_tm->tm_sec =3D bcd2bin(reg[0] & AC100_ALM_SEC_MASK);
> > + alrm_tm->tm_min =3D bcd2bin(reg[1] & AC100_ALM_MIN_MASK);
> > + alrm_tm->tm_hour =3D bcd2bin(reg[2] & AC100_ALM_HOU_MASK);
> > + alrm_tm->tm_wday =3D bcd2bin(reg[3] & AC100_ALM_WEE_MASK);
> > + alrm_tm->tm_mday =3D bcd2bin(reg[4] & AC100_ALM_DAY_MASK);
> > + alrm_tm->tm_mon =3D bcd2bin(reg[5] & AC100_ALM_MON_MASK) - 1;
> > + alrm_tm->tm_year =3D bcd2bin(reg[6] & AC100_ALM_YEA_MASK) +
> > + AC100_YEAR_OFF;
> > +
> > + return 0;
> > +}
> > +
> > +static int ac100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *=
alrm)
> > +{
> > + struct ac100_rtc_dev *chip =3D dev_get_drvdata(dev);
> > + struct regmap *regmap =3D chip->regmap;
> > + struct rtc_time *alrm_tm =3D &alrm->time;
> > + u16 reg[8];
> > + int year;
> > + int ret;
> > +
> > + /* our alarm has a limited year range... */
> > + year =3D alrm_tm->tm_year - AC100_YEAR_OFF;
> > + if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
> > + dev_err(dev, "alarm only supports year in range %d - %d\n",
> > + AC100_YEAR_MIN, AC100_YEAR_MAX);
> > + return -EINVAL;
> > + }
> > +
> > + /* convert to BCD */
> > + reg[0] =3D (bin2bcd(alrm_tm->tm_sec) & AC100_ALM_SEC_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[1] =3D (bin2bcd(alrm_tm->tm_min) & AC100_ALM_MIN_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[2] =3D (bin2bcd(alrm_tm->tm_hour) & AC100_ALM_HOU_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + /* Do not enable weekday alarm */
> > + reg[3] =3D bin2bcd(alrm_tm->tm_wday) & AC100_ALM_WEE_MASK;
> > + reg[4] =3D (bin2bcd(alrm_tm->tm_mday) & AC100_ALM_DAY_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[5] =3D (bin2bcd(alrm_tm->tm_mon + 1) & AC100_ALM_MON_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[6] =3D (bin2bcd(year) & AC100_ALM_YEA_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + /* trigger write */
> > + reg[7] =3D AC100_ALM_UPD_TRIGGER;
> > +
> > + ret =3D regmap_bulk_write(regmap, AC100_ALM_SEC, reg, 8);
> > + if (ret)
> > + return ret;
> > +
> > + return ac100_rtc_alarm_irq_enable(dev, alrm->enabled);
> > +}
> > +
> > +static irqreturn_t ac100_rtc_irq(int irq, void *data)
> > +{
> > + struct ac100_rtc_dev *chip =3D data;
> > + struct regmap *regmap =3D chip->regmap;
> > + unsigned int val =3D 0;
> > + int ret;
> > +
> > + mutex_lock(&chip->rtc->ops_lock);
> > +
> > + /* read status */
> > + ret =3D regmap_read(regmap, AC100_ALM_INT_STA, &val);
> > + if (ret)
> > + goto out;
> > +
> > + if (val & AC100_ALM_INT_ENABLE) {
> > + /* signal rtc framework */
> > + rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
> > +
> > + /* clear status */
> > + ret =3D regmap_write(regmap, AC100_ALM_INT_STA, val);
> > + if (ret)
> > + goto out;
> > +
> > + /* disable interrupt */
> > + ret =3D ac100_rtc_alarm_irq_enable(chip->dev, 0);
> > + if (ret)
> > + goto out;
> > + }
> > +
> > +out:
> > + mutex_unlock(&chip->rtc->ops_lock);
> > + return IRQ_HANDLED;
> > +}
> > +
> > +static const struct rtc_class_ops ac100_rtc_ops =3D {
> > + .read_time =3D ac100_rtc_get_time,
> > + .set_time =3D ac100_rtc_set_time,
> > + .read_alarm =3D ac100_rtc_get_alarm,
> > + .set_alarm =3D ac100_rtc_set_alarm,
> > + .alarm_irq_enable =3D ac100_rtc_alarm_irq_enable,
> > +};
> > +
> > +static int ac100_rtc_probe(struct platform_device *pdev)
> > +{
> > + struct ac100_dev *ac100 =3D dev_get_drvdata(pdev->dev.parent);
> > + struct ac100_rtc_dev *chip;
> > + int ret;
> > +
> > + chip =3D devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
> > + platform_set_drvdata(pdev, chip);
> > + chip->dev =3D &pdev->dev;
> > + chip->regmap =3D ac100->regmap;
> > +
> > + chip->irq =3D platform_get_irq(pdev, 0);
> > + if (chip->irq < 0) {
> > + dev_err(&pdev->dev, "No IRQ resource\n");
> > + return chip->irq;
> > + }
> > +
> > + ret =3D devm_request_threaded_irq(&pdev->dev, chip->irq, NULL,
> > + ac100_rtc_irq,
> > + IRQF_SHARED | IRQF_ONESHOT,
> > + dev_name(&pdev->dev), chip);
> > + if (ret) {
> > + dev_err(&pdev->dev, "Could not request IRQ\n");
> > + return ret;
> > + }
> > +
> > + /* always use 24 hour mode */
> > + regmap_write_bits(chip->regmap, AC100_RTC_CTRL, AC100_RTC_CTRL_24HOUR=
,
> > + AC100_RTC_CTRL_24HOUR);
> > +
> > + /* disable counter alarm interrupt */
> > + regmap_write(chip->regmap, AC100_ALM_INT_ENA, 0);
> > +
> > + /* clear counter alarm pending interrupts */
> > + regmap_write(chip->regmap, AC100_ALM_INT_STA, AC100_ALM_INT_ENABLE);
> > +
> > + chip->rtc =3D devm_rtc_device_register(&pdev->dev, "rtc-ac100",
> > + &ac100_rtc_ops, THIS_MODULE);
> > + if (IS_ERR(chip->rtc)) {
> > + dev_err(&pdev->dev, "unable to register device\n");
> > + return PTR_ERR(chip->rtc);
> > + }
> > +
> > + dev_info(&pdev->dev, "RTC enabled\n");
> > +
> > + return 0;
> > +}
> > +
> > +static const struct of_device_id ac100_rtc_match[] =3D {
> > + { .compatible =3D "x-powers,ac100-rtc" },
> > + { },
> > +};
> > +MODULE_DEVICE_TABLE(of, ac100_rtc_match);
> > +
> > +static struct platform_driver ac100_rtc_driver =3D {
> > + .probe =3D ac100_rtc_probe,
> > + .driver =3D {
> > + .name =3D "ac100-rtc",
> > + .of_match_table =3D of_match_ptr(ac100_rtc_match),
> > + },
> > +};
> > +module_platform_driver(ac100_rtc_driver);
> > +
> > +MODULE_DESCRIPTION("X-Powers AC100 RTC driver");
> > +MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
> > +MODULE_LICENSE("GPL v2");
>=20
--=20
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org =E2=94=82 Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
--=20
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
---=20
You received this message because you are subscribed to the Google Groups "=
rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to rtc-linux+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
WARNING: multiple messages have this Message-ID (diff)
From: lee.jones@linaro.org (Lee Jones)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v5 3/7] rtc: ac100: Add RTC driver for X-Powers AC100
Date: Mon, 8 Aug 2016 12:57:07 +0100 [thread overview]
Message-ID: <20160808115707.GP5243@dell> (raw)
In-Reply-To: <20160708162152.GM22202@piout.net>
On Fri, 08 Jul 2016, Alexandre Belloni wrote:
> On 08/07/2016 at 22:33:38 +0800, Chen-Yu Tsai wrote :
> > X-Powers AC100 is a codec / RTC combo chip. This driver supports
> > the RTC sub-device.
> >
> > The RTC block also has clock outputs and non-volatile storage.
> > Non-volatile storage wthin the RTC hardware is not supported.
> > Clock output support is added in the next patch.
> >
> > Signed-off-by: Chen-Yu Tsai <wens@csie.org>
> Acked-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Applied, thanks.
> > ---
> > Changes since v4:
> >
> > - None.
> >
> > Changes since v3:
> >
> > - Moved month/year conversion offsets inline with reg[] assignments
> > - Used platform_get_irq instead of of_get_irq
> > - Dropped separate mutex and use rtc->ops_lock in interrupt handler
> > - Dropped of_irq.h from includes
> > - Added of_device_id and .of_match_table
> >
> > Changes since v2:
> >
> > - Fixed off-by-1 errors in register mask macros
> > - Fixed copy-paste error in set_alarm function
> > - Drop uie_unsupported, since the alarm works now
> > - Fixed up copyright notice
> > - Changed license to GPL v2, matching mfd driver
> >
> > ---
> > drivers/rtc/Kconfig | 10 ++
> > drivers/rtc/Makefile | 1 +
> > drivers/rtc/rtc-ac100.c | 325 ++++++++++++++++++++++++++++++++++++++++++++++++
> > 3 files changed, 336 insertions(+)
> > create mode 100644 drivers/rtc/rtc-ac100.c
> >
> > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > index 18639e0cb6e2..b9d7cbb6bd76 100644
> > --- a/drivers/rtc/Kconfig
> > +++ b/drivers/rtc/Kconfig
> > @@ -183,6 +183,16 @@ config RTC_DRV_ABX80X
> > This driver can also be built as a module. If so, the module
> > will be called rtc-abx80x.
> >
> > +config RTC_DRV_AC100
> > + tristate "X-Powers AC100"
> > + depends on MFD_AC100
> > + help
> > + If you say yes here you get support for the real-time clock found
> > + in X-Powers AC100 family peripheral ICs.
> > +
> > + This driver can also be built as a module. If so, the module
> > + will be called rtc-ac100.
> > +
> > config RTC_DRV_AS3722
> > tristate "ams AS3722 RTC driver"
> > depends on MFD_AS3722
> > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> > index ea2833723fa9..b07c28779573 100644
> > --- a/drivers/rtc/Makefile
> > +++ b/drivers/rtc/Makefile
> > @@ -26,6 +26,7 @@ obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o
> > obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o
> > obj-$(CONFIG_RTC_DRV_ABB5ZES3) += rtc-ab-b5ze-s3.o
> > obj-$(CONFIG_RTC_DRV_ABX80X) += rtc-abx80x.o
> > +obj-$(CONFIG_RTC_DRV_AC100) += rtc-ac100.o
> > obj-$(CONFIG_RTC_DRV_ARMADA38X) += rtc-armada38x.o
> > obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o
> > obj-$(CONFIG_RTC_DRV_ASM9260) += rtc-asm9260.o
> > diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c
> > new file mode 100644
> > index 000000000000..5a9ca89d04c7
> > --- /dev/null
> > +++ b/drivers/rtc/rtc-ac100.c
> > @@ -0,0 +1,325 @@
> > +/*
> > + * RTC Driver for X-Powers AC100
> > + *
> > + * Copyright (c) 2016 Chen-Yu Tsai
> > + *
> > + * Chen-Yu Tsai <wens@csie.org>
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License version 2 as
> > + * published by the Free Software Foundation.
> > + *
> > + * 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/bcd.h>
> > +#include <linux/device.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/kernel.h>
> > +#include <linux/mfd/ac100.h>
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/of.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/regmap.h>
> > +#include <linux/rtc.h>
> > +#include <linux/types.h>
> > +
> > +/* Control register */
> > +#define AC100_RTC_CTRL_24HOUR BIT(0)
> > +
> > +/* RTC */
> > +#define AC100_RTC_SEC_MASK GENMASK(6, 0)
> > +#define AC100_RTC_MIN_MASK GENMASK(6, 0)
> > +#define AC100_RTC_HOU_MASK GENMASK(5, 0)
> > +#define AC100_RTC_WEE_MASK GENMASK(2, 0)
> > +#define AC100_RTC_DAY_MASK GENMASK(5, 0)
> > +#define AC100_RTC_MON_MASK GENMASK(4, 0)
> > +#define AC100_RTC_YEA_MASK GENMASK(7, 0)
> > +#define AC100_RTC_YEA_LEAP BIT(15)
> > +#define AC100_RTC_UPD_TRIGGER BIT(15)
> > +
> > +/* Alarm (wall clock) */
> > +#define AC100_ALM_INT_ENABLE BIT(0)
> > +
> > +#define AC100_ALM_SEC_MASK GENMASK(6, 0)
> > +#define AC100_ALM_MIN_MASK GENMASK(6, 0)
> > +#define AC100_ALM_HOU_MASK GENMASK(5, 0)
> > +#define AC100_ALM_WEE_MASK GENMASK(2, 0)
> > +#define AC100_ALM_DAY_MASK GENMASK(5, 0)
> > +#define AC100_ALM_MON_MASK GENMASK(4, 0)
> > +#define AC100_ALM_YEA_MASK GENMASK(7, 0)
> > +#define AC100_ALM_ENABLE_FLAG BIT(15)
> > +#define AC100_ALM_UPD_TRIGGER BIT(15)
> > +
> > +/*
> > + * The year parameter passed to the driver is usually an offset relative to
> > + * the year 1900. This macro is used to convert this offset to another one
> > + * relative to the minimum year allowed by the hardware.
> > + *
> > + * The year range is 1970 - 2069. This range is selected to match Allwinner's
> > + * driver.
> > + */
> > +#define AC100_YEAR_MIN 1970
> > +#define AC100_YEAR_MAX 2069
> > +#define AC100_YEAR_OFF (AC100_YEAR_MIN - 1900)
> > +
> > +struct ac100_rtc_dev {
> > + struct rtc_device *rtc;
> > + struct device *dev;
> > + struct regmap *regmap;
> > + int irq;
> > + unsigned long alarm;
> > +};
> > +
> > +static int ac100_rtc_get_time(struct device *dev, struct rtc_time *rtc_tm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + u16 reg[7];
> > + int ret;
> > +
> > + ret = regmap_bulk_read(regmap, AC100_RTC_SEC, reg, 7);
> > + if (ret)
> > + return ret;
> > +
> > + rtc_tm->tm_sec = bcd2bin(reg[0] & AC100_RTC_SEC_MASK);
> > + rtc_tm->tm_min = bcd2bin(reg[1] & AC100_RTC_MIN_MASK);
> > + rtc_tm->tm_hour = bcd2bin(reg[2] & AC100_RTC_HOU_MASK);
> > + rtc_tm->tm_wday = bcd2bin(reg[3] & AC100_RTC_WEE_MASK);
> > + rtc_tm->tm_mday = bcd2bin(reg[4] & AC100_RTC_DAY_MASK);
> > + rtc_tm->tm_mon = bcd2bin(reg[5] & AC100_RTC_MON_MASK) - 1;
> > + rtc_tm->tm_year = bcd2bin(reg[6] & AC100_RTC_YEA_MASK) +
> > + AC100_YEAR_OFF;
> > +
> > + return rtc_valid_tm(rtc_tm);
> > +}
> > +
> > +static int ac100_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + int year;
> > + u16 reg[8];
> > +
> > + /* our RTC has a limited year range... */
> > + year = rtc_tm->tm_year - AC100_YEAR_OFF;
> > + if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
> > + dev_err(dev, "rtc only supports year in range %d - %d\n",
> > + AC100_YEAR_MIN, AC100_YEAR_MAX);
> > + return -EINVAL;
> > + }
> > +
> > + /* convert to BCD */
> > + reg[0] = bin2bcd(rtc_tm->tm_sec) & AC100_RTC_SEC_MASK;
> > + reg[1] = bin2bcd(rtc_tm->tm_min) & AC100_RTC_MIN_MASK;
> > + reg[2] = bin2bcd(rtc_tm->tm_hour) & AC100_RTC_HOU_MASK;
> > + reg[3] = bin2bcd(rtc_tm->tm_wday) & AC100_RTC_WEE_MASK;
> > + reg[4] = bin2bcd(rtc_tm->tm_mday) & AC100_RTC_DAY_MASK;
> > + reg[5] = bin2bcd(rtc_tm->tm_mon + 1) & AC100_RTC_MON_MASK;
> > + reg[6] = bin2bcd(year) & AC100_RTC_YEA_MASK;
> > + /* trigger write */
> > + reg[7] = AC100_RTC_UPD_TRIGGER;
> > +
> > + /* Is it a leap year? */
> > + if (is_leap_year(year + AC100_YEAR_OFF + 1900))
> > + reg[6] |= AC100_RTC_YEA_LEAP;
> > +
> > + return regmap_bulk_write(regmap, AC100_RTC_SEC, reg, 8);
> > +}
> > +
> > +static int ac100_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + unsigned int val;
> > +
> > + val = en ? AC100_ALM_INT_ENABLE : 0;
> > +
> > + return regmap_write(regmap, AC100_ALM_INT_ENA, val);
> > +}
> > +
> > +static int ac100_rtc_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + struct rtc_time *alrm_tm = &alrm->time;
> > + u16 reg[7];
> > + unsigned int val;
> > + int ret;
> > +
> > + ret = regmap_read(regmap, AC100_ALM_INT_ENA, &val);
> > + if (ret)
> > + return ret;
> > +
> > + alrm->enabled = !!(val & AC100_ALM_INT_ENABLE);
> > +
> > + ret = regmap_bulk_read(regmap, AC100_ALM_SEC, reg, 7);
> > + if (ret)
> > + return ret;
> > +
> > + alrm_tm->tm_sec = bcd2bin(reg[0] & AC100_ALM_SEC_MASK);
> > + alrm_tm->tm_min = bcd2bin(reg[1] & AC100_ALM_MIN_MASK);
> > + alrm_tm->tm_hour = bcd2bin(reg[2] & AC100_ALM_HOU_MASK);
> > + alrm_tm->tm_wday = bcd2bin(reg[3] & AC100_ALM_WEE_MASK);
> > + alrm_tm->tm_mday = bcd2bin(reg[4] & AC100_ALM_DAY_MASK);
> > + alrm_tm->tm_mon = bcd2bin(reg[5] & AC100_ALM_MON_MASK) - 1;
> > + alrm_tm->tm_year = bcd2bin(reg[6] & AC100_ALM_YEA_MASK) +
> > + AC100_YEAR_OFF;
> > +
> > + return 0;
> > +}
> > +
> > +static int ac100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + struct rtc_time *alrm_tm = &alrm->time;
> > + u16 reg[8];
> > + int year;
> > + int ret;
> > +
> > + /* our alarm has a limited year range... */
> > + year = alrm_tm->tm_year - AC100_YEAR_OFF;
> > + if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
> > + dev_err(dev, "alarm only supports year in range %d - %d\n",
> > + AC100_YEAR_MIN, AC100_YEAR_MAX);
> > + return -EINVAL;
> > + }
> > +
> > + /* convert to BCD */
> > + reg[0] = (bin2bcd(alrm_tm->tm_sec) & AC100_ALM_SEC_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[1] = (bin2bcd(alrm_tm->tm_min) & AC100_ALM_MIN_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[2] = (bin2bcd(alrm_tm->tm_hour) & AC100_ALM_HOU_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + /* Do not enable weekday alarm */
> > + reg[3] = bin2bcd(alrm_tm->tm_wday) & AC100_ALM_WEE_MASK;
> > + reg[4] = (bin2bcd(alrm_tm->tm_mday) & AC100_ALM_DAY_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[5] = (bin2bcd(alrm_tm->tm_mon + 1) & AC100_ALM_MON_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[6] = (bin2bcd(year) & AC100_ALM_YEA_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + /* trigger write */
> > + reg[7] = AC100_ALM_UPD_TRIGGER;
> > +
> > + ret = regmap_bulk_write(regmap, AC100_ALM_SEC, reg, 8);
> > + if (ret)
> > + return ret;
> > +
> > + return ac100_rtc_alarm_irq_enable(dev, alrm->enabled);
> > +}
> > +
> > +static irqreturn_t ac100_rtc_irq(int irq, void *data)
> > +{
> > + struct ac100_rtc_dev *chip = data;
> > + struct regmap *regmap = chip->regmap;
> > + unsigned int val = 0;
> > + int ret;
> > +
> > + mutex_lock(&chip->rtc->ops_lock);
> > +
> > + /* read status */
> > + ret = regmap_read(regmap, AC100_ALM_INT_STA, &val);
> > + if (ret)
> > + goto out;
> > +
> > + if (val & AC100_ALM_INT_ENABLE) {
> > + /* signal rtc framework */
> > + rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
> > +
> > + /* clear status */
> > + ret = regmap_write(regmap, AC100_ALM_INT_STA, val);
> > + if (ret)
> > + goto out;
> > +
> > + /* disable interrupt */
> > + ret = ac100_rtc_alarm_irq_enable(chip->dev, 0);
> > + if (ret)
> > + goto out;
> > + }
> > +
> > +out:
> > + mutex_unlock(&chip->rtc->ops_lock);
> > + return IRQ_HANDLED;
> > +}
> > +
> > +static const struct rtc_class_ops ac100_rtc_ops = {
> > + .read_time = ac100_rtc_get_time,
> > + .set_time = ac100_rtc_set_time,
> > + .read_alarm = ac100_rtc_get_alarm,
> > + .set_alarm = ac100_rtc_set_alarm,
> > + .alarm_irq_enable = ac100_rtc_alarm_irq_enable,
> > +};
> > +
> > +static int ac100_rtc_probe(struct platform_device *pdev)
> > +{
> > + struct ac100_dev *ac100 = dev_get_drvdata(pdev->dev.parent);
> > + struct ac100_rtc_dev *chip;
> > + int ret;
> > +
> > + chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
> > + platform_set_drvdata(pdev, chip);
> > + chip->dev = &pdev->dev;
> > + chip->regmap = ac100->regmap;
> > +
> > + chip->irq = platform_get_irq(pdev, 0);
> > + if (chip->irq < 0) {
> > + dev_err(&pdev->dev, "No IRQ resource\n");
> > + return chip->irq;
> > + }
> > +
> > + ret = devm_request_threaded_irq(&pdev->dev, chip->irq, NULL,
> > + ac100_rtc_irq,
> > + IRQF_SHARED | IRQF_ONESHOT,
> > + dev_name(&pdev->dev), chip);
> > + if (ret) {
> > + dev_err(&pdev->dev, "Could not request IRQ\n");
> > + return ret;
> > + }
> > +
> > + /* always use 24 hour mode */
> > + regmap_write_bits(chip->regmap, AC100_RTC_CTRL, AC100_RTC_CTRL_24HOUR,
> > + AC100_RTC_CTRL_24HOUR);
> > +
> > + /* disable counter alarm interrupt */
> > + regmap_write(chip->regmap, AC100_ALM_INT_ENA, 0);
> > +
> > + /* clear counter alarm pending interrupts */
> > + regmap_write(chip->regmap, AC100_ALM_INT_STA, AC100_ALM_INT_ENABLE);
> > +
> > + chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-ac100",
> > + &ac100_rtc_ops, THIS_MODULE);
> > + if (IS_ERR(chip->rtc)) {
> > + dev_err(&pdev->dev, "unable to register device\n");
> > + return PTR_ERR(chip->rtc);
> > + }
> > +
> > + dev_info(&pdev->dev, "RTC enabled\n");
> > +
> > + return 0;
> > +}
> > +
> > +static const struct of_device_id ac100_rtc_match[] = {
> > + { .compatible = "x-powers,ac100-rtc" },
> > + { },
> > +};
> > +MODULE_DEVICE_TABLE(of, ac100_rtc_match);
> > +
> > +static struct platform_driver ac100_rtc_driver = {
> > + .probe = ac100_rtc_probe,
> > + .driver = {
> > + .name = "ac100-rtc",
> > + .of_match_table = of_match_ptr(ac100_rtc_match),
> > + },
> > +};
> > +module_platform_driver(ac100_rtc_driver);
> > +
> > +MODULE_DESCRIPTION("X-Powers AC100 RTC driver");
> > +MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
> > +MODULE_LICENSE("GPL v2");
>
--
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
WARNING: multiple messages have this Message-ID (diff)
From: Lee Jones <lee.jones-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
To: Alexandre Belloni
<alexandre.belloni-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org>
Cc: Chen-Yu Tsai <wens-jdAy2FN1RRM@public.gmane.org>,
Alessandro Zummo
<a.zummo-BfzFCNDTiLLj+vYz1yj4TQ@public.gmane.org>,
Maxime Ripard
<maxime.ripard-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org>,
linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
rtc-linux-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org,
devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
Subject: Re: [PATCH v5 3/7] rtc: ac100: Add RTC driver for X-Powers AC100
Date: Mon, 8 Aug 2016 12:57:07 +0100 [thread overview]
Message-ID: <20160808115707.GP5243@dell> (raw)
In-Reply-To: <20160708162152.GM22202-m++hUPXGwpdeoWH0uzbU5w@public.gmane.org>
On Fri, 08 Jul 2016, Alexandre Belloni wrote:
> On 08/07/2016 at 22:33:38 +0800, Chen-Yu Tsai wrote :
> > X-Powers AC100 is a codec / RTC combo chip. This driver supports
> > the RTC sub-device.
> >
> > The RTC block also has clock outputs and non-volatile storage.
> > Non-volatile storage wthin the RTC hardware is not supported.
> > Clock output support is added in the next patch.
> >
> > Signed-off-by: Chen-Yu Tsai <wens-jdAy2FN1RRM@public.gmane.org>
> Acked-by: Alexandre Belloni <alexandre.belloni-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org>
Applied, thanks.
> > ---
> > Changes since v4:
> >
> > - None.
> >
> > Changes since v3:
> >
> > - Moved month/year conversion offsets inline with reg[] assignments
> > - Used platform_get_irq instead of of_get_irq
> > - Dropped separate mutex and use rtc->ops_lock in interrupt handler
> > - Dropped of_irq.h from includes
> > - Added of_device_id and .of_match_table
> >
> > Changes since v2:
> >
> > - Fixed off-by-1 errors in register mask macros
> > - Fixed copy-paste error in set_alarm function
> > - Drop uie_unsupported, since the alarm works now
> > - Fixed up copyright notice
> > - Changed license to GPL v2, matching mfd driver
> >
> > ---
> > drivers/rtc/Kconfig | 10 ++
> > drivers/rtc/Makefile | 1 +
> > drivers/rtc/rtc-ac100.c | 325 ++++++++++++++++++++++++++++++++++++++++++++++++
> > 3 files changed, 336 insertions(+)
> > create mode 100644 drivers/rtc/rtc-ac100.c
> >
> > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > index 18639e0cb6e2..b9d7cbb6bd76 100644
> > --- a/drivers/rtc/Kconfig
> > +++ b/drivers/rtc/Kconfig
> > @@ -183,6 +183,16 @@ config RTC_DRV_ABX80X
> > This driver can also be built as a module. If so, the module
> > will be called rtc-abx80x.
> >
> > +config RTC_DRV_AC100
> > + tristate "X-Powers AC100"
> > + depends on MFD_AC100
> > + help
> > + If you say yes here you get support for the real-time clock found
> > + in X-Powers AC100 family peripheral ICs.
> > +
> > + This driver can also be built as a module. If so, the module
> > + will be called rtc-ac100.
> > +
> > config RTC_DRV_AS3722
> > tristate "ams AS3722 RTC driver"
> > depends on MFD_AS3722
> > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> > index ea2833723fa9..b07c28779573 100644
> > --- a/drivers/rtc/Makefile
> > +++ b/drivers/rtc/Makefile
> > @@ -26,6 +26,7 @@ obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o
> > obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o
> > obj-$(CONFIG_RTC_DRV_ABB5ZES3) += rtc-ab-b5ze-s3.o
> > obj-$(CONFIG_RTC_DRV_ABX80X) += rtc-abx80x.o
> > +obj-$(CONFIG_RTC_DRV_AC100) += rtc-ac100.o
> > obj-$(CONFIG_RTC_DRV_ARMADA38X) += rtc-armada38x.o
> > obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o
> > obj-$(CONFIG_RTC_DRV_ASM9260) += rtc-asm9260.o
> > diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c
> > new file mode 100644
> > index 000000000000..5a9ca89d04c7
> > --- /dev/null
> > +++ b/drivers/rtc/rtc-ac100.c
> > @@ -0,0 +1,325 @@
> > +/*
> > + * RTC Driver for X-Powers AC100
> > + *
> > + * Copyright (c) 2016 Chen-Yu Tsai
> > + *
> > + * Chen-Yu Tsai <wens-jdAy2FN1RRM@public.gmane.org>
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License version 2 as
> > + * published by the Free Software Foundation.
> > + *
> > + * 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/bcd.h>
> > +#include <linux/device.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/kernel.h>
> > +#include <linux/mfd/ac100.h>
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/of.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/regmap.h>
> > +#include <linux/rtc.h>
> > +#include <linux/types.h>
> > +
> > +/* Control register */
> > +#define AC100_RTC_CTRL_24HOUR BIT(0)
> > +
> > +/* RTC */
> > +#define AC100_RTC_SEC_MASK GENMASK(6, 0)
> > +#define AC100_RTC_MIN_MASK GENMASK(6, 0)
> > +#define AC100_RTC_HOU_MASK GENMASK(5, 0)
> > +#define AC100_RTC_WEE_MASK GENMASK(2, 0)
> > +#define AC100_RTC_DAY_MASK GENMASK(5, 0)
> > +#define AC100_RTC_MON_MASK GENMASK(4, 0)
> > +#define AC100_RTC_YEA_MASK GENMASK(7, 0)
> > +#define AC100_RTC_YEA_LEAP BIT(15)
> > +#define AC100_RTC_UPD_TRIGGER BIT(15)
> > +
> > +/* Alarm (wall clock) */
> > +#define AC100_ALM_INT_ENABLE BIT(0)
> > +
> > +#define AC100_ALM_SEC_MASK GENMASK(6, 0)
> > +#define AC100_ALM_MIN_MASK GENMASK(6, 0)
> > +#define AC100_ALM_HOU_MASK GENMASK(5, 0)
> > +#define AC100_ALM_WEE_MASK GENMASK(2, 0)
> > +#define AC100_ALM_DAY_MASK GENMASK(5, 0)
> > +#define AC100_ALM_MON_MASK GENMASK(4, 0)
> > +#define AC100_ALM_YEA_MASK GENMASK(7, 0)
> > +#define AC100_ALM_ENABLE_FLAG BIT(15)
> > +#define AC100_ALM_UPD_TRIGGER BIT(15)
> > +
> > +/*
> > + * The year parameter passed to the driver is usually an offset relative to
> > + * the year 1900. This macro is used to convert this offset to another one
> > + * relative to the minimum year allowed by the hardware.
> > + *
> > + * The year range is 1970 - 2069. This range is selected to match Allwinner's
> > + * driver.
> > + */
> > +#define AC100_YEAR_MIN 1970
> > +#define AC100_YEAR_MAX 2069
> > +#define AC100_YEAR_OFF (AC100_YEAR_MIN - 1900)
> > +
> > +struct ac100_rtc_dev {
> > + struct rtc_device *rtc;
> > + struct device *dev;
> > + struct regmap *regmap;
> > + int irq;
> > + unsigned long alarm;
> > +};
> > +
> > +static int ac100_rtc_get_time(struct device *dev, struct rtc_time *rtc_tm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + u16 reg[7];
> > + int ret;
> > +
> > + ret = regmap_bulk_read(regmap, AC100_RTC_SEC, reg, 7);
> > + if (ret)
> > + return ret;
> > +
> > + rtc_tm->tm_sec = bcd2bin(reg[0] & AC100_RTC_SEC_MASK);
> > + rtc_tm->tm_min = bcd2bin(reg[1] & AC100_RTC_MIN_MASK);
> > + rtc_tm->tm_hour = bcd2bin(reg[2] & AC100_RTC_HOU_MASK);
> > + rtc_tm->tm_wday = bcd2bin(reg[3] & AC100_RTC_WEE_MASK);
> > + rtc_tm->tm_mday = bcd2bin(reg[4] & AC100_RTC_DAY_MASK);
> > + rtc_tm->tm_mon = bcd2bin(reg[5] & AC100_RTC_MON_MASK) - 1;
> > + rtc_tm->tm_year = bcd2bin(reg[6] & AC100_RTC_YEA_MASK) +
> > + AC100_YEAR_OFF;
> > +
> > + return rtc_valid_tm(rtc_tm);
> > +}
> > +
> > +static int ac100_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + int year;
> > + u16 reg[8];
> > +
> > + /* our RTC has a limited year range... */
> > + year = rtc_tm->tm_year - AC100_YEAR_OFF;
> > + if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
> > + dev_err(dev, "rtc only supports year in range %d - %d\n",
> > + AC100_YEAR_MIN, AC100_YEAR_MAX);
> > + return -EINVAL;
> > + }
> > +
> > + /* convert to BCD */
> > + reg[0] = bin2bcd(rtc_tm->tm_sec) & AC100_RTC_SEC_MASK;
> > + reg[1] = bin2bcd(rtc_tm->tm_min) & AC100_RTC_MIN_MASK;
> > + reg[2] = bin2bcd(rtc_tm->tm_hour) & AC100_RTC_HOU_MASK;
> > + reg[3] = bin2bcd(rtc_tm->tm_wday) & AC100_RTC_WEE_MASK;
> > + reg[4] = bin2bcd(rtc_tm->tm_mday) & AC100_RTC_DAY_MASK;
> > + reg[5] = bin2bcd(rtc_tm->tm_mon + 1) & AC100_RTC_MON_MASK;
> > + reg[6] = bin2bcd(year) & AC100_RTC_YEA_MASK;
> > + /* trigger write */
> > + reg[7] = AC100_RTC_UPD_TRIGGER;
> > +
> > + /* Is it a leap year? */
> > + if (is_leap_year(year + AC100_YEAR_OFF + 1900))
> > + reg[6] |= AC100_RTC_YEA_LEAP;
> > +
> > + return regmap_bulk_write(regmap, AC100_RTC_SEC, reg, 8);
> > +}
> > +
> > +static int ac100_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + unsigned int val;
> > +
> > + val = en ? AC100_ALM_INT_ENABLE : 0;
> > +
> > + return regmap_write(regmap, AC100_ALM_INT_ENA, val);
> > +}
> > +
> > +static int ac100_rtc_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + struct rtc_time *alrm_tm = &alrm->time;
> > + u16 reg[7];
> > + unsigned int val;
> > + int ret;
> > +
> > + ret = regmap_read(regmap, AC100_ALM_INT_ENA, &val);
> > + if (ret)
> > + return ret;
> > +
> > + alrm->enabled = !!(val & AC100_ALM_INT_ENABLE);
> > +
> > + ret = regmap_bulk_read(regmap, AC100_ALM_SEC, reg, 7);
> > + if (ret)
> > + return ret;
> > +
> > + alrm_tm->tm_sec = bcd2bin(reg[0] & AC100_ALM_SEC_MASK);
> > + alrm_tm->tm_min = bcd2bin(reg[1] & AC100_ALM_MIN_MASK);
> > + alrm_tm->tm_hour = bcd2bin(reg[2] & AC100_ALM_HOU_MASK);
> > + alrm_tm->tm_wday = bcd2bin(reg[3] & AC100_ALM_WEE_MASK);
> > + alrm_tm->tm_mday = bcd2bin(reg[4] & AC100_ALM_DAY_MASK);
> > + alrm_tm->tm_mon = bcd2bin(reg[5] & AC100_ALM_MON_MASK) - 1;
> > + alrm_tm->tm_year = bcd2bin(reg[6] & AC100_ALM_YEA_MASK) +
> > + AC100_YEAR_OFF;
> > +
> > + return 0;
> > +}
> > +
> > +static int ac100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + struct rtc_time *alrm_tm = &alrm->time;
> > + u16 reg[8];
> > + int year;
> > + int ret;
> > +
> > + /* our alarm has a limited year range... */
> > + year = alrm_tm->tm_year - AC100_YEAR_OFF;
> > + if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
> > + dev_err(dev, "alarm only supports year in range %d - %d\n",
> > + AC100_YEAR_MIN, AC100_YEAR_MAX);
> > + return -EINVAL;
> > + }
> > +
> > + /* convert to BCD */
> > + reg[0] = (bin2bcd(alrm_tm->tm_sec) & AC100_ALM_SEC_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[1] = (bin2bcd(alrm_tm->tm_min) & AC100_ALM_MIN_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[2] = (bin2bcd(alrm_tm->tm_hour) & AC100_ALM_HOU_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + /* Do not enable weekday alarm */
> > + reg[3] = bin2bcd(alrm_tm->tm_wday) & AC100_ALM_WEE_MASK;
> > + reg[4] = (bin2bcd(alrm_tm->tm_mday) & AC100_ALM_DAY_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[5] = (bin2bcd(alrm_tm->tm_mon + 1) & AC100_ALM_MON_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[6] = (bin2bcd(year) & AC100_ALM_YEA_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + /* trigger write */
> > + reg[7] = AC100_ALM_UPD_TRIGGER;
> > +
> > + ret = regmap_bulk_write(regmap, AC100_ALM_SEC, reg, 8);
> > + if (ret)
> > + return ret;
> > +
> > + return ac100_rtc_alarm_irq_enable(dev, alrm->enabled);
> > +}
> > +
> > +static irqreturn_t ac100_rtc_irq(int irq, void *data)
> > +{
> > + struct ac100_rtc_dev *chip = data;
> > + struct regmap *regmap = chip->regmap;
> > + unsigned int val = 0;
> > + int ret;
> > +
> > + mutex_lock(&chip->rtc->ops_lock);
> > +
> > + /* read status */
> > + ret = regmap_read(regmap, AC100_ALM_INT_STA, &val);
> > + if (ret)
> > + goto out;
> > +
> > + if (val & AC100_ALM_INT_ENABLE) {
> > + /* signal rtc framework */
> > + rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
> > +
> > + /* clear status */
> > + ret = regmap_write(regmap, AC100_ALM_INT_STA, val);
> > + if (ret)
> > + goto out;
> > +
> > + /* disable interrupt */
> > + ret = ac100_rtc_alarm_irq_enable(chip->dev, 0);
> > + if (ret)
> > + goto out;
> > + }
> > +
> > +out:
> > + mutex_unlock(&chip->rtc->ops_lock);
> > + return IRQ_HANDLED;
> > +}
> > +
> > +static const struct rtc_class_ops ac100_rtc_ops = {
> > + .read_time = ac100_rtc_get_time,
> > + .set_time = ac100_rtc_set_time,
> > + .read_alarm = ac100_rtc_get_alarm,
> > + .set_alarm = ac100_rtc_set_alarm,
> > + .alarm_irq_enable = ac100_rtc_alarm_irq_enable,
> > +};
> > +
> > +static int ac100_rtc_probe(struct platform_device *pdev)
> > +{
> > + struct ac100_dev *ac100 = dev_get_drvdata(pdev->dev.parent);
> > + struct ac100_rtc_dev *chip;
> > + int ret;
> > +
> > + chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
> > + platform_set_drvdata(pdev, chip);
> > + chip->dev = &pdev->dev;
> > + chip->regmap = ac100->regmap;
> > +
> > + chip->irq = platform_get_irq(pdev, 0);
> > + if (chip->irq < 0) {
> > + dev_err(&pdev->dev, "No IRQ resource\n");
> > + return chip->irq;
> > + }
> > +
> > + ret = devm_request_threaded_irq(&pdev->dev, chip->irq, NULL,
> > + ac100_rtc_irq,
> > + IRQF_SHARED | IRQF_ONESHOT,
> > + dev_name(&pdev->dev), chip);
> > + if (ret) {
> > + dev_err(&pdev->dev, "Could not request IRQ\n");
> > + return ret;
> > + }
> > +
> > + /* always use 24 hour mode */
> > + regmap_write_bits(chip->regmap, AC100_RTC_CTRL, AC100_RTC_CTRL_24HOUR,
> > + AC100_RTC_CTRL_24HOUR);
> > +
> > + /* disable counter alarm interrupt */
> > + regmap_write(chip->regmap, AC100_ALM_INT_ENA, 0);
> > +
> > + /* clear counter alarm pending interrupts */
> > + regmap_write(chip->regmap, AC100_ALM_INT_STA, AC100_ALM_INT_ENABLE);
> > +
> > + chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-ac100",
> > + &ac100_rtc_ops, THIS_MODULE);
> > + if (IS_ERR(chip->rtc)) {
> > + dev_err(&pdev->dev, "unable to register device\n");
> > + return PTR_ERR(chip->rtc);
> > + }
> > +
> > + dev_info(&pdev->dev, "RTC enabled\n");
> > +
> > + return 0;
> > +}
> > +
> > +static const struct of_device_id ac100_rtc_match[] = {
> > + { .compatible = "x-powers,ac100-rtc" },
> > + { },
> > +};
> > +MODULE_DEVICE_TABLE(of, ac100_rtc_match);
> > +
> > +static struct platform_driver ac100_rtc_driver = {
> > + .probe = ac100_rtc_probe,
> > + .driver = {
> > + .name = "ac100-rtc",
> > + .of_match_table = of_match_ptr(ac100_rtc_match),
> > + },
> > +};
> > +module_platform_driver(ac100_rtc_driver);
> > +
> > +MODULE_DESCRIPTION("X-Powers AC100 RTC driver");
> > +MODULE_AUTHOR("Chen-Yu Tsai <wens-jdAy2FN1RRM@public.gmane.org>");
> > +MODULE_LICENSE("GPL v2");
>
--
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
--
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
---
You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rtc-linux+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
WARNING: multiple messages have this Message-ID (diff)
From: Lee Jones <lee.jones@linaro.org>
To: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: Chen-Yu Tsai <wens@csie.org>,
Alessandro Zummo <a.zummo@towertech.it>,
Maxime Ripard <maxime.ripard@free-electrons.com>,
linux-kernel@vger.kernel.org, rtc-linux@googlegroups.com,
devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH v5 3/7] rtc: ac100: Add RTC driver for X-Powers AC100
Date: Mon, 8 Aug 2016 12:57:07 +0100 [thread overview]
Message-ID: <20160808115707.GP5243@dell> (raw)
In-Reply-To: <20160708162152.GM22202@piout.net>
On Fri, 08 Jul 2016, Alexandre Belloni wrote:
> On 08/07/2016 at 22:33:38 +0800, Chen-Yu Tsai wrote :
> > X-Powers AC100 is a codec / RTC combo chip. This driver supports
> > the RTC sub-device.
> >
> > The RTC block also has clock outputs and non-volatile storage.
> > Non-volatile storage wthin the RTC hardware is not supported.
> > Clock output support is added in the next patch.
> >
> > Signed-off-by: Chen-Yu Tsai <wens@csie.org>
> Acked-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Applied, thanks.
> > ---
> > Changes since v4:
> >
> > - None.
> >
> > Changes since v3:
> >
> > - Moved month/year conversion offsets inline with reg[] assignments
> > - Used platform_get_irq instead of of_get_irq
> > - Dropped separate mutex and use rtc->ops_lock in interrupt handler
> > - Dropped of_irq.h from includes
> > - Added of_device_id and .of_match_table
> >
> > Changes since v2:
> >
> > - Fixed off-by-1 errors in register mask macros
> > - Fixed copy-paste error in set_alarm function
> > - Drop uie_unsupported, since the alarm works now
> > - Fixed up copyright notice
> > - Changed license to GPL v2, matching mfd driver
> >
> > ---
> > drivers/rtc/Kconfig | 10 ++
> > drivers/rtc/Makefile | 1 +
> > drivers/rtc/rtc-ac100.c | 325 ++++++++++++++++++++++++++++++++++++++++++++++++
> > 3 files changed, 336 insertions(+)
> > create mode 100644 drivers/rtc/rtc-ac100.c
> >
> > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > index 18639e0cb6e2..b9d7cbb6bd76 100644
> > --- a/drivers/rtc/Kconfig
> > +++ b/drivers/rtc/Kconfig
> > @@ -183,6 +183,16 @@ config RTC_DRV_ABX80X
> > This driver can also be built as a module. If so, the module
> > will be called rtc-abx80x.
> >
> > +config RTC_DRV_AC100
> > + tristate "X-Powers AC100"
> > + depends on MFD_AC100
> > + help
> > + If you say yes here you get support for the real-time clock found
> > + in X-Powers AC100 family peripheral ICs.
> > +
> > + This driver can also be built as a module. If so, the module
> > + will be called rtc-ac100.
> > +
> > config RTC_DRV_AS3722
> > tristate "ams AS3722 RTC driver"
> > depends on MFD_AS3722
> > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> > index ea2833723fa9..b07c28779573 100644
> > --- a/drivers/rtc/Makefile
> > +++ b/drivers/rtc/Makefile
> > @@ -26,6 +26,7 @@ obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o
> > obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o
> > obj-$(CONFIG_RTC_DRV_ABB5ZES3) += rtc-ab-b5ze-s3.o
> > obj-$(CONFIG_RTC_DRV_ABX80X) += rtc-abx80x.o
> > +obj-$(CONFIG_RTC_DRV_AC100) += rtc-ac100.o
> > obj-$(CONFIG_RTC_DRV_ARMADA38X) += rtc-armada38x.o
> > obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o
> > obj-$(CONFIG_RTC_DRV_ASM9260) += rtc-asm9260.o
> > diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c
> > new file mode 100644
> > index 000000000000..5a9ca89d04c7
> > --- /dev/null
> > +++ b/drivers/rtc/rtc-ac100.c
> > @@ -0,0 +1,325 @@
> > +/*
> > + * RTC Driver for X-Powers AC100
> > + *
> > + * Copyright (c) 2016 Chen-Yu Tsai
> > + *
> > + * Chen-Yu Tsai <wens@csie.org>
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License version 2 as
> > + * published by the Free Software Foundation.
> > + *
> > + * 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/bcd.h>
> > +#include <linux/device.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/kernel.h>
> > +#include <linux/mfd/ac100.h>
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/of.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/regmap.h>
> > +#include <linux/rtc.h>
> > +#include <linux/types.h>
> > +
> > +/* Control register */
> > +#define AC100_RTC_CTRL_24HOUR BIT(0)
> > +
> > +/* RTC */
> > +#define AC100_RTC_SEC_MASK GENMASK(6, 0)
> > +#define AC100_RTC_MIN_MASK GENMASK(6, 0)
> > +#define AC100_RTC_HOU_MASK GENMASK(5, 0)
> > +#define AC100_RTC_WEE_MASK GENMASK(2, 0)
> > +#define AC100_RTC_DAY_MASK GENMASK(5, 0)
> > +#define AC100_RTC_MON_MASK GENMASK(4, 0)
> > +#define AC100_RTC_YEA_MASK GENMASK(7, 0)
> > +#define AC100_RTC_YEA_LEAP BIT(15)
> > +#define AC100_RTC_UPD_TRIGGER BIT(15)
> > +
> > +/* Alarm (wall clock) */
> > +#define AC100_ALM_INT_ENABLE BIT(0)
> > +
> > +#define AC100_ALM_SEC_MASK GENMASK(6, 0)
> > +#define AC100_ALM_MIN_MASK GENMASK(6, 0)
> > +#define AC100_ALM_HOU_MASK GENMASK(5, 0)
> > +#define AC100_ALM_WEE_MASK GENMASK(2, 0)
> > +#define AC100_ALM_DAY_MASK GENMASK(5, 0)
> > +#define AC100_ALM_MON_MASK GENMASK(4, 0)
> > +#define AC100_ALM_YEA_MASK GENMASK(7, 0)
> > +#define AC100_ALM_ENABLE_FLAG BIT(15)
> > +#define AC100_ALM_UPD_TRIGGER BIT(15)
> > +
> > +/*
> > + * The year parameter passed to the driver is usually an offset relative to
> > + * the year 1900. This macro is used to convert this offset to another one
> > + * relative to the minimum year allowed by the hardware.
> > + *
> > + * The year range is 1970 - 2069. This range is selected to match Allwinner's
> > + * driver.
> > + */
> > +#define AC100_YEAR_MIN 1970
> > +#define AC100_YEAR_MAX 2069
> > +#define AC100_YEAR_OFF (AC100_YEAR_MIN - 1900)
> > +
> > +struct ac100_rtc_dev {
> > + struct rtc_device *rtc;
> > + struct device *dev;
> > + struct regmap *regmap;
> > + int irq;
> > + unsigned long alarm;
> > +};
> > +
> > +static int ac100_rtc_get_time(struct device *dev, struct rtc_time *rtc_tm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + u16 reg[7];
> > + int ret;
> > +
> > + ret = regmap_bulk_read(regmap, AC100_RTC_SEC, reg, 7);
> > + if (ret)
> > + return ret;
> > +
> > + rtc_tm->tm_sec = bcd2bin(reg[0] & AC100_RTC_SEC_MASK);
> > + rtc_tm->tm_min = bcd2bin(reg[1] & AC100_RTC_MIN_MASK);
> > + rtc_tm->tm_hour = bcd2bin(reg[2] & AC100_RTC_HOU_MASK);
> > + rtc_tm->tm_wday = bcd2bin(reg[3] & AC100_RTC_WEE_MASK);
> > + rtc_tm->tm_mday = bcd2bin(reg[4] & AC100_RTC_DAY_MASK);
> > + rtc_tm->tm_mon = bcd2bin(reg[5] & AC100_RTC_MON_MASK) - 1;
> > + rtc_tm->tm_year = bcd2bin(reg[6] & AC100_RTC_YEA_MASK) +
> > + AC100_YEAR_OFF;
> > +
> > + return rtc_valid_tm(rtc_tm);
> > +}
> > +
> > +static int ac100_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + int year;
> > + u16 reg[8];
> > +
> > + /* our RTC has a limited year range... */
> > + year = rtc_tm->tm_year - AC100_YEAR_OFF;
> > + if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
> > + dev_err(dev, "rtc only supports year in range %d - %d\n",
> > + AC100_YEAR_MIN, AC100_YEAR_MAX);
> > + return -EINVAL;
> > + }
> > +
> > + /* convert to BCD */
> > + reg[0] = bin2bcd(rtc_tm->tm_sec) & AC100_RTC_SEC_MASK;
> > + reg[1] = bin2bcd(rtc_tm->tm_min) & AC100_RTC_MIN_MASK;
> > + reg[2] = bin2bcd(rtc_tm->tm_hour) & AC100_RTC_HOU_MASK;
> > + reg[3] = bin2bcd(rtc_tm->tm_wday) & AC100_RTC_WEE_MASK;
> > + reg[4] = bin2bcd(rtc_tm->tm_mday) & AC100_RTC_DAY_MASK;
> > + reg[5] = bin2bcd(rtc_tm->tm_mon + 1) & AC100_RTC_MON_MASK;
> > + reg[6] = bin2bcd(year) & AC100_RTC_YEA_MASK;
> > + /* trigger write */
> > + reg[7] = AC100_RTC_UPD_TRIGGER;
> > +
> > + /* Is it a leap year? */
> > + if (is_leap_year(year + AC100_YEAR_OFF + 1900))
> > + reg[6] |= AC100_RTC_YEA_LEAP;
> > +
> > + return regmap_bulk_write(regmap, AC100_RTC_SEC, reg, 8);
> > +}
> > +
> > +static int ac100_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + unsigned int val;
> > +
> > + val = en ? AC100_ALM_INT_ENABLE : 0;
> > +
> > + return regmap_write(regmap, AC100_ALM_INT_ENA, val);
> > +}
> > +
> > +static int ac100_rtc_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + struct rtc_time *alrm_tm = &alrm->time;
> > + u16 reg[7];
> > + unsigned int val;
> > + int ret;
> > +
> > + ret = regmap_read(regmap, AC100_ALM_INT_ENA, &val);
> > + if (ret)
> > + return ret;
> > +
> > + alrm->enabled = !!(val & AC100_ALM_INT_ENABLE);
> > +
> > + ret = regmap_bulk_read(regmap, AC100_ALM_SEC, reg, 7);
> > + if (ret)
> > + return ret;
> > +
> > + alrm_tm->tm_sec = bcd2bin(reg[0] & AC100_ALM_SEC_MASK);
> > + alrm_tm->tm_min = bcd2bin(reg[1] & AC100_ALM_MIN_MASK);
> > + alrm_tm->tm_hour = bcd2bin(reg[2] & AC100_ALM_HOU_MASK);
> > + alrm_tm->tm_wday = bcd2bin(reg[3] & AC100_ALM_WEE_MASK);
> > + alrm_tm->tm_mday = bcd2bin(reg[4] & AC100_ALM_DAY_MASK);
> > + alrm_tm->tm_mon = bcd2bin(reg[5] & AC100_ALM_MON_MASK) - 1;
> > + alrm_tm->tm_year = bcd2bin(reg[6] & AC100_ALM_YEA_MASK) +
> > + AC100_YEAR_OFF;
> > +
> > + return 0;
> > +}
> > +
> > +static int ac100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> > +{
> > + struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
> > + struct regmap *regmap = chip->regmap;
> > + struct rtc_time *alrm_tm = &alrm->time;
> > + u16 reg[8];
> > + int year;
> > + int ret;
> > +
> > + /* our alarm has a limited year range... */
> > + year = alrm_tm->tm_year - AC100_YEAR_OFF;
> > + if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
> > + dev_err(dev, "alarm only supports year in range %d - %d\n",
> > + AC100_YEAR_MIN, AC100_YEAR_MAX);
> > + return -EINVAL;
> > + }
> > +
> > + /* convert to BCD */
> > + reg[0] = (bin2bcd(alrm_tm->tm_sec) & AC100_ALM_SEC_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[1] = (bin2bcd(alrm_tm->tm_min) & AC100_ALM_MIN_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[2] = (bin2bcd(alrm_tm->tm_hour) & AC100_ALM_HOU_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + /* Do not enable weekday alarm */
> > + reg[3] = bin2bcd(alrm_tm->tm_wday) & AC100_ALM_WEE_MASK;
> > + reg[4] = (bin2bcd(alrm_tm->tm_mday) & AC100_ALM_DAY_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[5] = (bin2bcd(alrm_tm->tm_mon + 1) & AC100_ALM_MON_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + reg[6] = (bin2bcd(year) & AC100_ALM_YEA_MASK) |
> > + AC100_ALM_ENABLE_FLAG;
> > + /* trigger write */
> > + reg[7] = AC100_ALM_UPD_TRIGGER;
> > +
> > + ret = regmap_bulk_write(regmap, AC100_ALM_SEC, reg, 8);
> > + if (ret)
> > + return ret;
> > +
> > + return ac100_rtc_alarm_irq_enable(dev, alrm->enabled);
> > +}
> > +
> > +static irqreturn_t ac100_rtc_irq(int irq, void *data)
> > +{
> > + struct ac100_rtc_dev *chip = data;
> > + struct regmap *regmap = chip->regmap;
> > + unsigned int val = 0;
> > + int ret;
> > +
> > + mutex_lock(&chip->rtc->ops_lock);
> > +
> > + /* read status */
> > + ret = regmap_read(regmap, AC100_ALM_INT_STA, &val);
> > + if (ret)
> > + goto out;
> > +
> > + if (val & AC100_ALM_INT_ENABLE) {
> > + /* signal rtc framework */
> > + rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
> > +
> > + /* clear status */
> > + ret = regmap_write(regmap, AC100_ALM_INT_STA, val);
> > + if (ret)
> > + goto out;
> > +
> > + /* disable interrupt */
> > + ret = ac100_rtc_alarm_irq_enable(chip->dev, 0);
> > + if (ret)
> > + goto out;
> > + }
> > +
> > +out:
> > + mutex_unlock(&chip->rtc->ops_lock);
> > + return IRQ_HANDLED;
> > +}
> > +
> > +static const struct rtc_class_ops ac100_rtc_ops = {
> > + .read_time = ac100_rtc_get_time,
> > + .set_time = ac100_rtc_set_time,
> > + .read_alarm = ac100_rtc_get_alarm,
> > + .set_alarm = ac100_rtc_set_alarm,
> > + .alarm_irq_enable = ac100_rtc_alarm_irq_enable,
> > +};
> > +
> > +static int ac100_rtc_probe(struct platform_device *pdev)
> > +{
> > + struct ac100_dev *ac100 = dev_get_drvdata(pdev->dev.parent);
> > + struct ac100_rtc_dev *chip;
> > + int ret;
> > +
> > + chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
> > + platform_set_drvdata(pdev, chip);
> > + chip->dev = &pdev->dev;
> > + chip->regmap = ac100->regmap;
> > +
> > + chip->irq = platform_get_irq(pdev, 0);
> > + if (chip->irq < 0) {
> > + dev_err(&pdev->dev, "No IRQ resource\n");
> > + return chip->irq;
> > + }
> > +
> > + ret = devm_request_threaded_irq(&pdev->dev, chip->irq, NULL,
> > + ac100_rtc_irq,
> > + IRQF_SHARED | IRQF_ONESHOT,
> > + dev_name(&pdev->dev), chip);
> > + if (ret) {
> > + dev_err(&pdev->dev, "Could not request IRQ\n");
> > + return ret;
> > + }
> > +
> > + /* always use 24 hour mode */
> > + regmap_write_bits(chip->regmap, AC100_RTC_CTRL, AC100_RTC_CTRL_24HOUR,
> > + AC100_RTC_CTRL_24HOUR);
> > +
> > + /* disable counter alarm interrupt */
> > + regmap_write(chip->regmap, AC100_ALM_INT_ENA, 0);
> > +
> > + /* clear counter alarm pending interrupts */
> > + regmap_write(chip->regmap, AC100_ALM_INT_STA, AC100_ALM_INT_ENABLE);
> > +
> > + chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-ac100",
> > + &ac100_rtc_ops, THIS_MODULE);
> > + if (IS_ERR(chip->rtc)) {
> > + dev_err(&pdev->dev, "unable to register device\n");
> > + return PTR_ERR(chip->rtc);
> > + }
> > +
> > + dev_info(&pdev->dev, "RTC enabled\n");
> > +
> > + return 0;
> > +}
> > +
> > +static const struct of_device_id ac100_rtc_match[] = {
> > + { .compatible = "x-powers,ac100-rtc" },
> > + { },
> > +};
> > +MODULE_DEVICE_TABLE(of, ac100_rtc_match);
> > +
> > +static struct platform_driver ac100_rtc_driver = {
> > + .probe = ac100_rtc_probe,
> > + .driver = {
> > + .name = "ac100-rtc",
> > + .of_match_table = of_match_ptr(ac100_rtc_match),
> > + },
> > +};
> > +module_platform_driver(ac100_rtc_driver);
> > +
> > +MODULE_DESCRIPTION("X-Powers AC100 RTC driver");
> > +MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
> > +MODULE_LICENSE("GPL v2");
>
--
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
next prev parent reply other threads:[~2016-08-08 11:55 UTC|newest]
Thread overview: 80+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-08 14:33 [rtc-linux] [PATCH v5 0/7] mfd: ac100: Add support for X-Powers AC100 audio codec / RTC combo IC Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` [rtc-linux] [PATCH v5 1/7] mfd: ac100: Add device tree bindings for X-Powers AC100 codec/RTC " Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-08-08 11:50 ` [rtc-linux] " Lee Jones
2016-08-08 11:50 ` Lee Jones
2016-08-08 11:50 ` Lee Jones
2016-08-08 11:56 ` [rtc-linux] " Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-07-08 14:33 ` [rtc-linux] [PATCH v5 2/7] mfd: ac100: Add driver for X-Powers AC100 audio codec / RTC " Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-08-08 11:56 ` [rtc-linux] " Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-07-08 14:33 ` [rtc-linux] [PATCH v5 3/7] rtc: ac100: Add RTC driver for X-Powers AC100 Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 16:21 ` [rtc-linux] " Alexandre Belloni
2016-07-08 16:21 ` Alexandre Belloni
2016-07-08 16:21 ` Alexandre Belloni
2016-07-08 16:21 ` Alexandre Belloni
2016-08-08 11:57 ` Lee Jones [this message]
2016-08-08 11:57 ` Lee Jones
2016-08-08 11:57 ` Lee Jones
2016-08-08 11:57 ` Lee Jones
2017-02-05 21:33 ` [v5,3/7] " Rask Ingemann Lambertsen
2017-02-05 21:33 ` Rask Ingemann Lambertsen
2017-02-06 3:29 ` Chen-Yu Tsai
2017-02-06 3:29 ` Chen-Yu Tsai
2017-02-06 18:15 ` Rask Ingemann Lambertsen
2017-02-06 18:15 ` Rask Ingemann Lambertsen
2016-07-08 14:33 ` [rtc-linux] [PATCH v5 4/7] rtc: ac100: Add clk output support Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 16:29 ` [rtc-linux] " Alexandre Belloni
2016-07-08 16:29 ` Alexandre Belloni
2016-07-08 16:29 ` Alexandre Belloni
2016-07-08 16:29 ` Alexandre Belloni
2016-08-08 11:56 ` [rtc-linux] " Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-08-08 11:56 ` Lee Jones
2016-07-08 14:33 ` [rtc-linux] [PATCH v5 5/7] ARM: dts: sun9i: a80-optimus: Add device node for AC100 Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` [rtc-linux] [PATCH v5 6/7] ARM: dts: sun9i: cubieboard4: " Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` [rtc-linux] [PATCH v5 7/7] ARM: dts: sun9i: Switch to the AC100 RTC clock outputs for osc32k Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-08 14:33 ` Chen-Yu Tsai
2016-07-11 6:50 ` [rtc-linux] " Maxime Ripard
2016-07-11 6:50 ` Maxime Ripard
2016-07-11 6:50 ` Maxime Ripard
2016-07-11 6:50 ` Maxime Ripard
2016-07-12 2:02 ` [rtc-linux] " Chen-Yu Tsai
2016-07-12 2:02 ` Chen-Yu Tsai
2016-07-12 2:02 ` Chen-Yu Tsai
2016-08-16 7:20 ` [rtc-linux] " Chen-Yu Tsai
2016-08-16 7:20 ` Chen-Yu Tsai
2016-08-16 7:20 ` Chen-Yu Tsai
2016-08-16 7:20 ` Chen-Yu Tsai
2016-08-08 12:01 ` [rtc-linux] Re: [PATCH v5 0/7] mfd: ac100: Add support for X-Powers AC100 audio codec / RTC combo IC Lee Jones
2016-08-08 12:01 ` Lee Jones
2016-08-08 12:01 ` Lee Jones
2016-08-08 12:01 ` Lee Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160808115707.GP5243@dell \
--to=lee.jones@linaro.org \
--cc=a.zummo@towertech.it \
--cc=alexandre.belloni@free-electrons.com \
--cc=devicetree@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maxime.ripard@free-electrons.com \
--cc=rtc-linux@googlegroups.com \
--cc=wens@csie.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.