All of lore.kernel.org
 help / color / mirror / Atom feed
From: Brian Masney <masneyb@onstation.org>
To: jic23@kernel.org, linux-iio@vger.kernel.org
Cc: gregkh@linuxfoundation.org, devel@driverdev.osuosl.org,
	knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net,
	linux-kernel@vger.kernel.org, Jon.Brenner@ams.com
Subject: [PATCH 03/12] staging: iio: tsl2x7x: add common function for reading chip status
Date: Sat,  3 Mar 2018 20:49:33 -0500	[thread overview]
Message-ID: <20180304014942.18727-4-masneyb@onstation.org> (raw)
In-Reply-To: <20180304014942.18727-1-masneyb@onstation.org>

There were three places where the same chunk of code was used to read
the chip status. This patch creates a common function
tsl2x7x_read_status() to reduce duplicate code. This patch also corrects
tsl2x7x_event_handler() to properly check for an error after reading the
chip status.

Signed-off-by: Brian Masney <masneyb@onstation.org>
---
 drivers/staging/iio/light/tsl2x7x.c | 40 ++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c
index c02db03ef369..6bb622816660 100644
--- a/drivers/staging/iio/light/tsl2x7x.c
+++ b/drivers/staging/iio/light/tsl2x7x.c
@@ -293,6 +293,20 @@ static int tsl2x7x_clear_interrupts(struct tsl2X7X_chip *chip, int reg)
 	return ret;
 }
 
+static int tsl2x7x_read_status(struct tsl2X7X_chip *chip)
+{
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(chip->client,
+				       TSL2X7X_CMD_REG | TSL2X7X_STATUS);
+	if (ret < 0)
+		dev_err(&chip->client->dev,
+			"%s: failed to read STATUS register: %d\n", __func__,
+			ret);
+
+	return ret;
+}
+
 /**
  * tsl2x7x_get_lux() - Reads and calculates current lux value.
  * @indio_dev:	pointer to IIO device
@@ -332,13 +346,10 @@ static int tsl2x7x_get_lux(struct iio_dev *indio_dev)
 		goto out_unlock;
 	}
 
-	ret = i2c_smbus_read_byte_data(chip->client,
-				       TSL2X7X_CMD_REG | TSL2X7X_STATUS);
-	if (ret < 0) {
-		dev_err(&chip->client->dev,
-			"%s: Failed to read STATUS Reg\n", __func__);
+	ret = tsl2x7x_read_status(chip);
+	if (ret < 0)
 		goto out_unlock;
-	}
+
 	/* is data new & valid */
 	if (!(ret & TSL2X7X_STA_ADC_VALID)) {
 		dev_err(&chip->client->dev,
@@ -458,12 +469,9 @@ static int tsl2x7x_get_prox(struct iio_dev *indio_dev)
 		return -EBUSY;
 	}
 
-	ret = i2c_smbus_read_byte_data(chip->client,
-				       TSL2X7X_CMD_REG | TSL2X7X_STATUS);
-	if (ret < 0) {
-		dev_err(&chip->client->dev, "i2c err=%d\n", ret);
+	ret = tsl2x7x_read_status(chip);
+	if (ret < 0)
 		goto prox_poll_err;
-	}
 
 	switch (chip->id) {
 	case tsl2571:
@@ -1396,13 +1404,13 @@ static irqreturn_t tsl2x7x_event_handler(int irq, void *private)
 	struct tsl2X7X_chip *chip = iio_priv(indio_dev);
 	s64 timestamp = iio_get_time_ns(indio_dev);
 	int ret;
-	u8 value;
 
-	value = i2c_smbus_read_byte_data(chip->client,
-					 TSL2X7X_CMD_REG | TSL2X7X_STATUS);
+	ret = tsl2x7x_read_status(chip);
+	if (ret < 0)
+		return ret;
 
 	/* What type of interrupt do we need to process */
-	if (value & TSL2X7X_STA_PRX_INTR) {
+	if (ret & TSL2X7X_STA_PRX_INTR) {
 		tsl2x7x_get_prox(indio_dev); /* freshen data for ABI */
 		iio_push_event(indio_dev,
 			       IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
@@ -1412,7 +1420,7 @@ static irqreturn_t tsl2x7x_event_handler(int irq, void *private)
 						    timestamp);
 	}
 
-	if (value & TSL2X7X_STA_ALS_INTR) {
+	if (ret & TSL2X7X_STA_ALS_INTR) {
 		tsl2x7x_get_lux(indio_dev); /* freshen data for ABI */
 		iio_push_event(indio_dev,
 			       IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
-- 
2.14.3

  parent reply	other threads:[~2018-03-04  1:49 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-04  1:49 [PATCH 00/12] staging cleanups Brian Masney
2018-03-04  1:49 ` [PATCH 01/12] staging: iio: tsl2x7x: remove power functions from tsl2X7X_platform_data Brian Masney
2018-03-10 14:20   ` Jonathan Cameron
2018-03-10 14:20     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 02/12] staging: iio: tsl2x7x: add common function for clearing interrupts Brian Masney
2018-03-10 14:27   ` Jonathan Cameron
2018-03-10 14:27     ` Jonathan Cameron
2018-03-04  1:49 ` Brian Masney [this message]
2018-03-10 14:34   ` [PATCH 03/12] staging: iio: tsl2x7x: add common function for reading chip status Jonathan Cameron
2018-03-10 14:34     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 04/12] staging: iio: tsl2x7x: add common function for writing to the control register Brian Masney
2018-03-10 14:36   ` Jonathan Cameron
2018-03-10 14:36     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 05/12] staging: iio: tsl2x7x: convert mutex_trylock() to mutex_lock() Brian Masney
2018-03-10 14:39   ` Jonathan Cameron
2018-03-10 14:39     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 06/12] staging: iio: tsl2x7x: correctly return errors in tsl2x7x_get_prox() Brian Masney
2018-03-10 14:42   ` Jonathan Cameron
2018-03-10 14:42     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 07/12] staging: iio: tsl2x7x: correct 'Avoid CamelCase' warning from checkpatch Brian Masney
2018-03-10 14:43   ` Jonathan Cameron
2018-03-10 14:43     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 08/12] staging: iio: tsl2x7x: add error handling to tsl2x7x_prox_cal() Brian Masney
2018-03-10 14:44   ` Jonathan Cameron
2018-03-10 14:44     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 09/12] staging: iio: tsl2x7x: add missing error checks Brian Masney
2018-03-10 14:45   ` Jonathan Cameron
2018-03-10 14:45     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 10/12] staging: iio: tsl2x7x: make logging consistent and correct newlines Brian Masney
2018-03-10 14:52   ` Jonathan Cameron
2018-03-10 14:52     ` Jonathan Cameron
2018-03-11 13:45     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 11/12] staging: iio: tsl2x7x: remove unnecessary sysfs attribute Brian Masney
2018-03-10 14:54   ` Jonathan Cameron
2018-03-10 14:54     ` Jonathan Cameron
2018-03-04  1:49 ` [PATCH 12/12] staging: iio: tsl2x7x: make proximity sensor function correctly Brian Masney
2018-03-10 14:56   ` Jonathan Cameron

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=20180304014942.18727-4-masneyb@onstation.org \
    --to=masneyb@onstation.org \
    --cc=Jon.Brenner@ams.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    /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.