All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org, "Nuno Sá" <noname.nuno@gmail.com>,
	"Astrid Rost" <astrid.rost@axis.com>,
	"Matti Vaittinen" <mazziesaccount@gmail.com>,
	"Per-Daniel Olsson" <perdaniel.olsson@axis.com>,
	"Javier Carrasco" <javier.carrasco.cruz@gmail.com>,
	"Subhajit Ghosh" <subhajit.ghosh@tweaklogic.com>
Cc: "Nuno Sá" <nuno.sa@analog.com>,
	"David Lechner" <dlechner@baylibre.com>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: [PATCH 07/18] iio: light: ltr501: Factor out core of write_raw() where direct mode claim is held.
Date: Sun,  9 Mar 2025 17:06:22 +0000	[thread overview]
Message-ID: <20250309170633.1347476-8-jic23@kernel.org> (raw)
In-Reply-To: <20250309170633.1347476-1-jic23@kernel.org>

From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Factoring this code out allows for direct returns on error simplifying code
flow.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 drivers/iio/light/ltr501.c | 94 +++++++++++++++++++-------------------
 1 file changed, 46 insertions(+), 48 deletions(-)

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index b42df15b36e1..c44c852a7d76 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -763,18 +763,14 @@ static int ltr501_get_gain_index(const struct ltr501_gain *gain, int size,
 	return -1;
 }
 
-static int ltr501_write_raw(struct iio_dev *indio_dev,
-			    struct iio_chan_spec const *chan,
-			    int val, int val2, long mask)
+static int __ltr501_write_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, int val2, long mask)
 {
 	struct ltr501_data *data = iio_priv(indio_dev);
 	int i, ret, freq_val, freq_val2;
 	const struct ltr501_chip_info *info = data->chip_info;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
-
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
@@ -782,53 +778,43 @@ static int ltr501_write_raw(struct iio_dev *indio_dev,
 			i = ltr501_get_gain_index(info->als_gain,
 						  info->als_gain_tbl_size,
 						  val, val2);
-			if (i < 0) {
-				ret = -EINVAL;
-				break;
-			}
+			if (i < 0)
+				return -EINVAL;
 
 			data->als_contr &= ~info->als_gain_mask;
 			data->als_contr |= i << info->als_gain_shift;
 
-			ret = regmap_write(data->regmap, LTR501_ALS_CONTR,
-					   data->als_contr);
-			break;
+			return regmap_write(data->regmap, LTR501_ALS_CONTR,
+					    data->als_contr);
 		case IIO_PROXIMITY:
 			i = ltr501_get_gain_index(info->ps_gain,
 						  info->ps_gain_tbl_size,
 						  val, val2);
-			if (i < 0) {
-				ret = -EINVAL;
-				break;
-			}
+			if (i < 0)
+				return -EINVAL;
+
 			data->ps_contr &= ~LTR501_CONTR_PS_GAIN_MASK;
 			data->ps_contr |= i << LTR501_CONTR_PS_GAIN_SHIFT;
 
-			ret = regmap_write(data->regmap, LTR501_PS_CONTR,
-					   data->ps_contr);
-			break;
+			return regmap_write(data->regmap, LTR501_PS_CONTR,
+					    data->ps_contr);
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 
 	case IIO_CHAN_INFO_INT_TIME:
 		switch (chan->type) {
 		case IIO_INTENSITY:
-			if (val != 0) {
-				ret = -EINVAL;
-				break;
-			}
+			if (val != 0)
+				return -EINVAL;
+
 			mutex_lock(&data->lock_als);
 			ret = ltr501_set_it_time(data, val2);
 			mutex_unlock(&data->lock_als);
-			break;
+			return ret;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		switch (chan->type) {
@@ -836,50 +822,62 @@ static int ltr501_write_raw(struct iio_dev *indio_dev,
 			ret = ltr501_als_read_samp_freq(data, &freq_val,
 							&freq_val2);
 			if (ret < 0)
-				break;
+				return ret;
 
 			ret = ltr501_als_write_samp_freq(data, val, val2);
 			if (ret < 0)
-				break;
+				return ret;
 
 			/* update persistence count when changing frequency */
 			ret = ltr501_write_intr_prst(data, chan->type,
 						     0, data->als_period);
 
 			if (ret < 0)
-				ret = ltr501_als_write_samp_freq(data, freq_val,
-								 freq_val2);
-			break;
+				/* Do not ovewrite error */
+				ltr501_als_write_samp_freq(data, freq_val,
+							   freq_val2);
+			return ret;
 		case IIO_PROXIMITY:
 			ret = ltr501_ps_read_samp_freq(data, &freq_val,
 						       &freq_val2);
 			if (ret < 0)
-				break;
+				return ret;
 
 			ret = ltr501_ps_write_samp_freq(data, val, val2);
 			if (ret < 0)
-				break;
+				return ret;
 
 			/* update persistence count when changing frequency */
 			ret = ltr501_write_intr_prst(data, chan->type,
 						     0, data->ps_period);
 
 			if (ret < 0)
-				ret = ltr501_ps_write_samp_freq(data, freq_val,
-								freq_val2);
-			break;
+				/* Do not overwrite error */
+				ltr501_ps_write_samp_freq(data, freq_val,
+							  freq_val2);
+			return ret;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
-
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
+}
+
+static int ltr501_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
+{
+	int ret;
+
+	ret = iio_device_claim_direct_mode(indio_dev);
+	if (ret)
+		return ret;
+
+	ret = __ltr501_write_raw(indio_dev, chan, val, val2, mask);
 
 	iio_device_release_direct_mode(indio_dev);
+
 	return ret;
 }
 
-- 
2.48.1


  parent reply	other threads:[~2025-03-09 17:07 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-09 17:06 [PATCH 00/18] iio: light: Sparse friendly claim of direct mode Jonathan Cameron
2025-03-09 17:06 ` [PATCH 01/18] iio: light: apds9306: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-10  6:35   ` Subhajit Ghosh
2025-03-10 20:01     ` Jonathan Cameron
2025-03-15 16:18       ` Subhajit Ghosh
2025-03-09 17:06 ` [PATCH 02/18] iio: light: gp2ap020a00f: " Jonathan Cameron
2025-03-09 17:06 ` [PATCH 03/18] iio: light: isl29125: " Jonathan Cameron
2025-03-09 17:06 ` [PATCH 04/18] iio: light: as73211: Use guard() and move mode switch into inner write_raw fucntion Jonathan Cameron
2025-03-11 18:55   ` David Lechner
2025-03-09 17:06 ` [PATCH 05/18] iio: light: as73211: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-09 17:06 ` [PATCH 06/18] iio: light: ltr501: Factor out IIO_INFO_RAW leg of read_raw() callback Jonathan Cameron
2025-03-09 17:06 ` Jonathan Cameron [this message]
2025-03-09 17:06 ` [PATCH 08/18] iio: light: ltr501: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-09 17:06 ` [PATCH 09/18] iio: light: opt4060: " Jonathan Cameron
2025-03-18  8:10   ` Per-Daniel Olsson
2025-03-18 17:07     ` Per-Daniel Olsson
2025-03-09 17:06 ` [PATCH 10/18] iio: light: rohm-bu27034: " Jonathan Cameron
2025-03-10  5:21   ` Matti Vaittinen
2025-03-09 17:06 ` [PATCH 11/18] iio: light: rpr0521: Factor out handling of IIO_INFO_RAW and use guard() Jonathan Cameron
2025-03-09 17:06 ` [PATCH 12/18] iio: light: rpr0521: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-09 17:06 ` [PATCH 13/18] iio: light: si1145: " Jonathan Cameron
2025-03-09 17:06 ` [PATCH 14/18] iio: light: st_uvis25: " Jonathan Cameron
2025-03-09 17:06 ` [PATCH 15/18] iio: light: tcs3414: " Jonathan Cameron
2025-03-09 17:06 ` [PATCH 16/18] iio: light: tcs3472: " Jonathan Cameron
2025-03-09 17:06 ` [PATCH 17/18] iio: light: vcnl4000: " Jonathan Cameron
2025-03-11 19:11   ` David Lechner
2025-03-31 11:44     ` Jonathan Cameron
2025-04-02  7:28       ` Astrid Rost
2025-03-18 17:08   ` Per-Daniel Olsson
2025-03-09 17:06 ` [PATCH 18/18] iio: light: vcnl4035: " Jonathan Cameron
2025-03-11 19:16 ` [PATCH 00/18] iio: light: Sparse friendly claim of direct mode David Lechner

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=20250309170633.1347476-8-jic23@kernel.org \
    --to=jic23@kernel.org \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=astrid.rost@axis.com \
    --cc=dlechner@baylibre.com \
    --cc=javier.carrasco.cruz@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=mazziesaccount@gmail.com \
    --cc=noname.nuno@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=perdaniel.olsson@axis.com \
    --cc=subhajit.ghosh@tweaklogic.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 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.