mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joshua Clayton <stillcompiling@gmail.com>
To: Alessandro Zummo <a.zummo@towertech.it>,
	Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org,
	Joshua Clayton <stillcompiling@gmail.com>
Subject: [PATCH 2/9] rtc-pcf2123: clean up reads from the chip
Date: Wed,  4 Nov 2015 07:36:33 -0800	[thread overview]
Message-ID: <d13636dfaabcaf43d4409068bdd6b579d34b0de4.1446587705.git.stillcompiling@gmail.com> (raw)
In-Reply-To: <cover.1446587705.git.stillcompiling@gmail.com>
In-Reply-To: <cover.1446587705.git.stillcompiling@gmail.com>

put read operations into a function.
This makes the starting register more prominent and hides the delay.

Signed-off-by: Joshua Clayton <stillcompiling@gmail.com>
---
 drivers/rtc/rtc-pcf2123.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/drivers/rtc/rtc-pcf2123.c b/drivers/rtc/rtc-pcf2123.c
index 7756210..648cb74 100644
--- a/drivers/rtc/rtc-pcf2123.c
+++ b/drivers/rtc/rtc-pcf2123.c
@@ -104,12 +104,26 @@ static inline void pcf2123_delay_trec(void)
 	ndelay(30);
 }
 
+static int pcf2123_read(struct device *dev, u8 reg, u8 *rxbuf, size_t size)
+{
+	struct spi_device *spi = to_spi_device(dev);
+	int ret;
+
+	if (reg > PCF2123_REG_MAX)
+		return -EFAULT;
+
+	reg |= PCF2123_READ;
+	ret = spi_write_then_read(spi, &reg, 1, rxbuf, size);
+	pcf2123_delay_trec();
+
+	return ret;
+}
+
 static ssize_t pcf2123_show(struct device *dev, struct device_attribute *attr,
 			    char *buffer)
 {
-	struct spi_device *spi = to_spi_device(dev);
 	struct pcf2123_sysfs_reg *r;
-	u8 txbuf[1], rxbuf[1];
+	u8 rxbuf[1];
 	unsigned long reg;
 	int ret;
 
@@ -119,11 +133,10 @@ static ssize_t pcf2123_show(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	txbuf[0] = PCF2123_READ | reg;
-	ret = spi_write_then_read(spi, txbuf, 1, rxbuf, 1);
+	ret = pcf2123_read(dev, reg, rxbuf, 1);
 	if (ret < 0)
 		return -EIO;
-	pcf2123_delay_trec();
+
 	return sprintf(buffer, "0x%x\n", rxbuf[0]);
 }
 
@@ -158,16 +171,12 @@ static ssize_t pcf2123_store(struct device *dev, struct device_attribute *attr,
 
 static int pcf2123_rtc_read_time(struct device *dev, struct rtc_time *tm)
 {
-	struct spi_device *spi = to_spi_device(dev);
-	u8 txbuf[1], rxbuf[7];
+	u8 rxbuf[7];
 	int ret;
 
-	txbuf[0] = PCF2123_READ | PCF2123_REG_SC;
-	ret = spi_write_then_read(spi, txbuf, sizeof(txbuf),
-			rxbuf, sizeof(rxbuf));
+	ret = pcf2123_read(dev, PCF2123_REG_SC, rxbuf, sizeof(rxbuf));
 	if (ret < 0)
 		return ret;
-	pcf2123_delay_trec();
 
 	tm->tm_sec = bcd2bin(rxbuf[0] & 0x7F);
 	tm->tm_min = bcd2bin(rxbuf[1] & 0x7F);
@@ -279,16 +288,12 @@ static int pcf2123_probe(struct spi_device *spi)
 	pcf2123_delay_trec();
 
 	/* See if the counter was actually stopped */
-	txbuf[0] = PCF2123_READ | PCF2123_REG_CTRL1;
-	dev_dbg(&spi->dev, "checking for presence of RTC (0x%02X)\n",
-			txbuf[0]);
-	ret = spi_write_then_read(spi, txbuf, 1 * sizeof(u8),
-					rxbuf, 2 * sizeof(u8));
+	dev_dbg(&spi->dev, "checking for presence of RTC\n");
+	ret = pcf2123_read(&spi->dev, PCF2123_REG_CTRL1, rxbuf, sizeof(rxbuf));
 	dev_dbg(&spi->dev, "received data from RTC (0x%02X 0x%02X)\n",
 			rxbuf[0], rxbuf[1]);
 	if (ret < 0)
 		goto kfree_exit;
-	pcf2123_delay_trec();
 
 	if (!(rxbuf[0] & 0x20)) {
 		dev_err(&spi->dev, "chip not found\n");
-- 
2.5.0


  parent reply	other threads:[~2015-11-04 15:37 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-04 15:36 [PATCH 0/9] rtc-2123: access the clock offset feature Joshua Clayton
2015-11-04 15:36 ` [PATCH 1/9] rtc-pcf2123: Document all registers and useful bits Joshua Clayton
2015-11-24 21:51   ` Alexandre Belloni
2015-12-01 18:13     ` Joshua Clayton
2015-11-04 15:36 ` Joshua Clayton [this message]
2015-11-04 15:36 ` [PATCH 3/9] rtc-pcf2123: clean up writes to the rtc chip Joshua Clayton
2015-11-24 22:16   ` Alexandre Belloni
2015-12-01 18:19     ` Joshua Clayton
2015-11-04 15:36 ` [PATCH 4/9] rtc-pcf2123: replace magic numbers with defines Joshua Clayton
2015-11-04 15:36 ` [PATCH 5/9] rtc-pcf2123: put the chip reset into a function Joshua Clayton
2015-11-24 23:17   ` Alexandre Belloni
2015-12-01 18:22     ` Joshua Clayton
2015-11-04 15:36 ` [PATCH 6/9] rtc-pcf2123: avoid resetting the clock if possible Joshua Clayton
2015-11-24 23:25   ` Alexandre Belloni
2015-12-01 20:23     ` Joshua Clayton
2015-12-01 21:04       ` Alexandre Belloni
2015-11-04 15:36 ` [PATCH 7/9] rtc-pcf2123: allow sysfs to accept hexidecimal Joshua Clayton
2015-11-04 15:36 ` [PATCH 8/9] rtc-pcf2123: use sysfs groups Joshua Clayton
2015-11-18 23:52   ` Joshua Clayton
2015-11-24 23:31   ` Alexandre Belloni
2015-12-01 20:28     ` Joshua Clayton
2015-12-01 20:47       ` Alexandre Belloni
2015-11-04 15:36 ` [PATCH 9/9] rtc-pcf2123: adjust the clock rate via sysfs Joshua Clayton
2015-11-18 23:51   ` Joshua Clayton
2015-11-17 15:30 ` [PATCH 0/9] rtc-2123: access the clock offset feature Joshua Clayton
2015-11-17 16:25   ` Alexandre Belloni
2015-11-19  0:25     ` Joshua Clayton

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=d13636dfaabcaf43d4409068bdd6b579d34b0de4.1446587705.git.stillcompiling@gmail.com \
    --to=stillcompiling@gmail.com \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@free-electrons.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rtc-linux@googlegroups.com \
    /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

Powered by JetHome