linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Meerwald <pmeerw@pmeerw.net>
To: linux-iio@vger.kernel.org
Cc: lars@metafoo.de, jic23@kernel.org, marek.belisko@gmail.com,
	shubhrajyoti@ti.com, Peter Meerwald <pmeerw@pmeerw.net>
Subject: [PATCH 4/9] iio: rework sampling rate setting in hmc5843
Date: Tue, 15 May 2012 00:27:29 +0200	[thread overview]
Message-ID: <1337034454-29717-5-git-send-email-pmeerw@pmeerw.net> (raw)
In-Reply-To: <1337034454-29717-1-git-send-email-pmeerw@pmeerw.net>

From: Peter Meerwald <p.meerwald@bct-electronic.com>

Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
---
 drivers/staging/iio/magnetometer/hmc5843.c |   51 +++++++++++++---------------
 1 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/drivers/staging/iio/magnetometer/hmc5843.c b/drivers/staging/iio/magnetometer/hmc5843.c
index 1318f7d..6888e04 100644
--- a/drivers/staging/iio/magnetometer/hmc5843.c
+++ b/drivers/staging/iio/magnetometer/hmc5843.c
@@ -85,14 +85,6 @@
  */
 #define HMC5843_RATE_OFFSET			0x02
 #define HMC5843_RATE_BITMASK			0x1C
-#define RATE_5					0x00
-#define RATE_10					0x01
-#define RATE_20					0x02
-#define RATE_50					0x03
-#define RATE_100				0x04
-#define RATE_200				0x05
-#define RATE_500				0x06
-
 #define HMC5843_RATE_NOT_USED			0x07
 
 /*
@@ -342,6 +334,21 @@ static s32 hmc5843_set_rate(struct i2c_client *client,
 	return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
 }
 
+static int hmc5843_check_sampling_frequency(struct hmc5843_data *data,
+						const char *buf)
+{
+	const char * const *samp_freq = regval_to_samp_freq;
+	int i;
+
+	for (i = 0; i < HMC5843_RATE_NOT_USED; i++) {
+		if (strncmp(buf, samp_freq[i],
+			strlen(samp_freq[i])) == 0)
+			return i;
+	}
+
+	return -EINVAL;
+}
+
 static ssize_t set_sampling_frequency(struct device *dev,
 					struct device_attribute *attr,
 					const char *buf, size_t count)
@@ -350,27 +357,17 @@ static ssize_t set_sampling_frequency(struct device *dev,
 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
 	struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
 	struct hmc5843_data *data = iio_priv(indio_dev);
-	unsigned long rate = 0;
-
-	if (strncmp(buf, "0.5" , 3) == 0)
-		rate = RATE_5;
-	else if (strncmp(buf, "1" , 1) == 0)
-		rate = RATE_10;
-	else if (strncmp(buf, "2", 1) == 0)
-		rate = RATE_20;
-	else if (strncmp(buf, "5", 1) == 0)
-		rate = RATE_50;
-	else if (strncmp(buf, "10", 2) == 0)
-		rate = RATE_100;
-	else if (strncmp(buf, "20" , 2) == 0)
-		rate = RATE_200;
-	else if (strncmp(buf, "50" , 2) == 0)
-		rate = RATE_500;
-	else
-		return -EINVAL;
+	int rate;
+
+	rate = hmc5843_check_sampling_frequency(data, buf);
+	if (rate < 0) {
+		dev_err(&client->dev,
+			"sampling frequency is not supported\n");
+		return rate;
+	}
 
 	mutex_lock(&data->lock);
-	dev_dbg(dev, "set rate to %lu\n", rate);
+	dev_dbg(dev, "set rate to %d\n", rate);
 	if (hmc5843_set_rate(client, rate)) {
 		count = -EINVAL;
 		goto exit;
-- 
1.7.5.4


  parent reply	other threads:[~2012-05-14 22:29 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-14 22:27 iio: v3, add support for HMC5883/HMC5883L to HMC5843 driver Peter Meerwald
2012-05-14 22:27 ` [PATCH 1/9] iio: fix access to hmc5843 private data Peter Meerwald
2012-05-15  8:29   ` Jonathan Cameron
2012-05-14 22:27 ` [PATCH 2/9] iio: change strict_strtoul() to kstrtoul() in hmc5843 Peter Meerwald
2012-05-14 22:27 ` [PATCH 3/9] iio: rename and prefix CONSTANTs to distinguish between HMC5843 and HMC5883 Peter Meerwald
2012-05-14 22:27 ` Peter Meerwald [this message]
2012-05-14 22:27 ` [PATCH 5/9] iio: add check for measurement configuration value passed to hmc5843 Peter Meerwald
2012-05-14 22:27 ` [PATCH 6/9] iio: cleanup and move comments in hmc5843 Peter Meerwald
2012-05-14 22:27 ` [PATCH 7/9] iio: rename function/data to consistently start with hmc5843_ Peter Meerwald
2012-05-14 22:27 ` [PATCH 8/9] iio: reorganize hmc5843_set_rate() Peter Meerwald
2012-05-15  8:39   ` Jonathan Cameron
2012-05-14 22:27 ` [PATCH 9/9] iio: add support for hmc5883/hmc5883l to hmc5843 magnetometer driver Peter Meerwald
2012-05-15 12:03   ` Jonathan Cameron
2012-05-15 15:30     ` Peter Meerwald
  -- strict thread matches above, loose matches on Subject: below --
2012-05-16 16:22 iio, add support for HMC5883/HMC5883L to HMC5843 driver Peter Meerwald
2012-05-16 16:22 ` [PATCH 4/9] iio: rework sampling rate setting in hmc5843 Peter Meerwald
2012-05-17  0:28 iio, add support for HMC5883/HMC5883L to HMC5843 driver Peter Meerwald
2012-05-17  0:28 ` [PATCH 4/9] iio: rework sampling rate setting in hmc5843 Peter Meerwald

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=1337034454-29717-5-git-send-email-pmeerw@pmeerw.net \
    --to=pmeerw@pmeerw.net \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=marek.belisko@gmail.com \
    --cc=shubhrajyoti@ti.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;
as well as URLs for NNTP newsgroup(s).