linux-rtc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Howey, Dylan" <Dylan.Howey@tennantco.com>
To: "alexandre.belloni@bootlin.com" <alexandre.belloni@bootlin.com>
Cc: "a.zummo@towertech.it" <a.zummo@towertech.it>,
	"linux-rtc@vger.kernel.org" <linux-rtc@vger.kernel.org>,
	"Howey, Dylan" <Dylan.Howey@tennantco.com>
Subject: [PATCH 2/2] Add alarm support to rtc-pcf2123
Date: Fri, 26 Apr 2019 19:37:15 +0000	[thread overview]
Message-ID: <20190426193648.1599-2-Dylan.Howey@tennantco.com> (raw)
In-Reply-To: <20190426193648.1599-1-Dylan.Howey@tennantco.com>

Allows alarm to be set using the wakealarm sysfs interface.

Signed-off-by: Dylan Howey <Dylan.Howey@tennantco.com>
---
 drivers/rtc/rtc-pcf2123.c | 115 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)

diff --git a/drivers/rtc/rtc-pcf2123.c b/drivers/rtc/rtc-pcf2123.c
index 4bfcb1e67c9b..8c16a313980d 100644
--- a/drivers/rtc/rtc-pcf2123.c
+++ b/drivers/rtc/rtc-pcf2123.c
@@ -308,6 +308,107 @@ static int pcf2123_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	return 0;
 }
 
+static int pcf2123_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
+{
+	struct pcf2123_plat_data *pdata = dev_get_platdata(dev);
+	u8 rxbuf[4];
+	int ret;
+	unsigned int val = 0;
+
+	ret = regmap_bulk_read(pdata->map, PCF2123_REG_ALRM_MN, rxbuf, 4);
+	if (ret)
+		return ret;
+
+	alm->time.tm_min = bcd2bin(rxbuf[0] & 0x7F);
+	alm->time.tm_hour = bcd2bin(rxbuf[1] & 0x3F);
+	alm->time.tm_mday = bcd2bin(rxbuf[2] & 0x3F);
+	alm->time.tm_wday = bcd2bin(rxbuf[3] & 0x07);
+
+	dev_dbg(dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, mon=%d, year=%d, wday=%d\n",
+			__func__,
+			alm->time.tm_sec, alm->time.tm_min, alm->time.tm_hour,
+			alm->time.tm_mday, alm->time.tm_mon, alm->time.tm_year,
+			alm->time.tm_wday);
+
+	ret = regmap_read(pdata->map, PCF2123_REG_CTRL2, &val);
+	if (ret)
+		return ret;
+
+	alm->enabled = !!(val & CTRL2_AIE);
+
+	return 0;
+}
+
+static int pcf2123_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
+{
+	struct pcf2123_plat_data *pdata = dev_get_platdata(dev);
+	u8 txbuf[4];
+	int ret;
+	unsigned int val = 0;
+
+	dev_dbg(dev, "%s: alm is secs=%d, mins=%d, hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
+			__func__,
+			alm->time.tm_sec, alm->time.tm_min, alm->time.tm_hour,
+			alm->time.tm_mday, alm->time.tm_mon, alm->time.tm_year,
+			alm->time.tm_wday);
+
+	/* Disable alarm */
+	ret = regmap_read(pdata->map, PCF2123_REG_CTRL2, &val);
+	if (ret)
+		return ret;
+
+	val &= ~(CTRL2_AIE);
+	ret = regmap_write(pdata->map, PCF2123_REG_CTRL2, val);
+
+	/* Set new alarm */
+	txbuf[0] = bin2bcd(alm->time.tm_min & 0x7F);
+	txbuf[1] = bin2bcd(alm->time.tm_hour & 0x3F);
+	txbuf[2] = bin2bcd(alm->time.tm_mday & 0x3F);
+	txbuf[3] = bin2bcd(alm->time.tm_wday & 0x07);
+
+	ret = regmap_bulk_write(pdata->map, PCF2123_REG_ALRM_MN, txbuf, 4);
+	if (ret)
+		return ret;
+
+	if (alm->enabled)	{
+		ret = regmap_read(pdata->map, PCF2123_REG_CTRL2, &val);
+		if (ret)
+			return ret;
+
+		val |= CTRL2_AIE;
+		ret = regmap_write(pdata->map, PCF2123_REG_CTRL2, val);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static irqreturn_t pcf2123_rtc_irq(int irq, void *dev)
+{
+	struct pcf2123_plat_data *pdata = dev_get_platdata(dev);
+	struct mutex *lock = &pdata->rtc->ops_lock;
+	unsigned int val = 0;
+	int ret = IRQ_NONE;
+
+	mutex_lock(lock);
+	regmap_read(pdata->map, PCF2123_REG_CTRL2, &val);
+
+	/* Alarm? */
+	if (val & CTRL2_AF) {
+		ret = IRQ_HANDLED;
+
+		val &= ~(CTRL2_AF);
+		regmap_write(pdata->map, PCF2123_REG_CTRL2, val);
+
+		rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
+	}
+
+	mutex_unlock(lock);
+
+	return ret;
+}
+
 static int pcf2123_reset(struct device *dev)
 {
 	struct pcf2123_plat_data *pdata = dev_get_platdata(dev);
@@ -347,6 +448,8 @@ static const struct rtc_class_ops pcf2123_rtc_ops = {
 	.set_time	= pcf2123_rtc_set_time,
 	.read_offset	= pcf2123_read_offset,
 	.set_offset	= pcf2123_set_offset,
+	.read_alarm = pcf2123_rtc_read_alarm,
+	.set_alarm = pcf2123_rtc_set_alarm,
 
 };
 
@@ -391,6 +494,8 @@ static int pcf2123_probe(struct spi_device *spi)
 	dev_info(&spi->dev, "spiclk %u KHz.\n",
 			(spi->max_speed_hz + 500) / 1000);
 
+	device_init_wakeup(&spi->dev, true);
+
 	/* Finalize the initialization */
 	rtc = devm_rtc_device_register(&spi->dev, pcf2123_driver.driver.name,
 			&pcf2123_rtc_ops, THIS_MODULE);
@@ -418,6 +523,16 @@ static int pcf2123_probe(struct spi_device *spi)
 		}
 	}
 
+	/* Register alarm irq */
+	if (spi->irq >= 0)	{
+		ret = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
+				pcf2123_rtc_irq,
+				IRQF_TRIGGER_LOW | IRQF_ONESHOT,
+				pcf2123_driver.driver.name, &spi->dev);
+		if (ret)
+			dev_err(&spi->dev, "could not request irq.\n");
+	}
+
 	return 0;
 
 sysfs_exit:
-- 
2.17.1


  reply	other threads:[~2019-04-26 19:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-26 19:37 [PATCH 1/2] Port rtc-pcf2123 to regmap Howey, Dylan
2019-04-26 19:37 ` Howey, Dylan [this message]
2019-04-27 13:11   ` [PATCH 2/2] Add alarm support to rtc-pcf2123 Alexandre Belloni
2019-04-27 13:00 ` [PATCH 1/2] Port rtc-pcf2123 to regmap Alexandre Belloni
2019-04-29 15:09   ` Howey, Dylan
2019-04-30  9:22     ` Alexandre Belloni
2019-05-02 17:45       ` Dylan Howey
2019-05-02 20:55         ` Alexandre Belloni
2019-05-03 13:30           ` Dylan Howey
2019-06-19 13:16             ` Alexandre Belloni

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=20190426193648.1599-2-Dylan.Howey@tennantco.com \
    --to=dylan.howey@tennantco.com \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-rtc@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).