From: Sandhya Bankar <bankarsandhya512@gmail.com>
To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org,
knaack.h@gmx.de, pmeerw@pmeerw.net, gregkh@linuxfoundation.org
Cc: outreachy-kernel@googlegroups.com
Subject: [PATCH v2] Staging: iio: meter: ade7753: Replace IIO_DEV_ATTR_SAMP_FREQ attribute with IIO_CHAN_INFO_SAMP_FREQ handlers.
Date: Thu, 29 Sep 2016 21:31:33 +0530 [thread overview]
Message-ID: <20160929160133.GA5460@localhost.localdomain> (raw)
Moved functionality from IIO_DEV_ATTR_SAMP_FREQ attribute into
IIO_CHAN_INFO_SAMP_FREQ handlers. Added ade7753_read_raw() and
ade7753_write_raw() to allow reading/writing the element as well.
Signed-off-by: Sandhya Bankar <bankarsandhya512@gmail.com>
---
Changes in v2:
* Adding "Staging" keyword in subject line.
drivers/staging/iio/meter/ade7753.c | 155 +++++++++++++++++++++---------------
1 file changed, 90 insertions(+), 65 deletions(-)
diff --git a/drivers/staging/iio/meter/ade7753.c b/drivers/staging/iio/meter/ade7753.c
index 4b5f05f..a2b4a18 100644
--- a/drivers/staging/iio/meter/ade7753.c
+++ b/drivers/staging/iio/meter/ade7753.c
@@ -98,6 +98,94 @@ static int ade7753_spi_read_reg_16(struct device *dev,
return 0;
}
+static int ade7753_read_raw_samp_freq(struct device *dev, int *val)
+{
+ int ret;
+ u16 t;
+
+ ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, &t);
+ if (ret)
+ return ret;
+ t = (t >> 11) & 0x3;
+ *val = 27900 / (1 + t);
+
+ return 0;
+}
+
+static int ade7753_write_raw_samp_freq(struct device *dev, int val)
+{
+ struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+ struct ade7753_state *st = iio_priv(indio_dev);
+ int ret;
+ u16 reg, t;
+
+ if (!val)
+ return -EINVAL;
+
+ t = 27900 / val;
+ if (t > 0)
+ t--;
+
+ if (t > 1)
+ st->us->max_speed_hz = ADE7753_SPI_SLOW;
+ else
+ st->us->max_speed_hz = ADE7753_SPI_FAST;
+
+ ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, ®);
+ if (ret)
+ goto out;
+
+ reg &= ~(3 << 11);
+ reg |= t << 11;
+
+ ret = ade7753_spi_write_reg_16(dev, ADE7753_MODE, reg);
+
+out:
+ return ret;
+}
+
+static int ade7753_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ if (val2)
+ return -EINVAL;
+ mutex_lock(&indio_dev->mlock);
+ ret = ade7753_write_raw_samp_freq(&indio_dev->dev, val);
+ mutex_unlock(&indio_dev->mlock);
+ return ret;
+ default:
+ return -EINVAL;
+ }
+
+ return ret;
+}
+
+static int ade7753_read_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int *val,
+ int *val2,
+ long mask)
+{
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ mutex_lock(&indio_dev->mlock);
+ ret = ade7753_read_raw_samp_freq(&indio_dev->dev, val);
+ mutex_unlock(&indio_dev->mlock);
+ return ret;
+ default:
+ return -EINVAL;
+ }
+
+ return ret;
+}
+
static int ade7753_spi_read_reg_24(struct device *dev,
u8 reg_address,
u32 *val)
@@ -383,82 +471,17 @@ err_ret:
return ret;
}
-static ssize_t ade7753_read_frequency(struct device *dev,
- struct device_attribute *attr,
- char *buf)
-{
- int ret;
- u16 t;
- int sps;
-
- ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, &t);
- if (ret)
- return ret;
-
- t = (t >> 11) & 0x3;
- sps = 27900 / (1 + t);
-
- return sprintf(buf, "%d\n", sps);
-}
-
-static ssize_t ade7753_write_frequency(struct device *dev,
- struct device_attribute *attr,
- const char *buf,
- size_t len)
-{
- struct iio_dev *indio_dev = dev_to_iio_dev(dev);
- struct ade7753_state *st = iio_priv(indio_dev);
- u16 val;
- int ret;
- u16 reg, t;
-
- ret = kstrtou16(buf, 10, &val);
- if (ret)
- return ret;
- if (!val)
- return -EINVAL;
-
- mutex_lock(&indio_dev->mlock);
-
- t = 27900 / val;
- if (t > 0)
- t--;
-
- if (t > 1)
- st->us->max_speed_hz = ADE7753_SPI_SLOW;
- else
- st->us->max_speed_hz = ADE7753_SPI_FAST;
-
- ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, ®);
- if (ret)
- goto out;
-
- reg &= ~(3 << 11);
- reg |= t << 11;
-
- ret = ade7753_spi_write_reg_16(dev, ADE7753_MODE, reg);
-
-out:
- mutex_unlock(&indio_dev->mlock);
-
- return ret ? ret : len;
-}
static IIO_DEV_ATTR_TEMP_RAW(ade7753_read_8bit);
static IIO_CONST_ATTR(in_temp_offset, "-25 C");
static IIO_CONST_ATTR(in_temp_scale, "0.67 C");
-static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
- ade7753_read_frequency,
- ade7753_write_frequency);
-
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
static struct attribute *ade7753_attributes[] = {
&iio_dev_attr_in_temp_raw.dev_attr.attr,
&iio_const_attr_in_temp_offset.dev_attr.attr,
&iio_const_attr_in_temp_scale.dev_attr.attr,
- &iio_dev_attr_sampling_frequency.dev_attr.attr,
&iio_const_attr_sampling_frequency_available.dev_attr.attr,
&iio_dev_attr_phcal.dev_attr.attr,
&iio_dev_attr_cfden.dev_attr.attr,
@@ -496,6 +519,8 @@ static const struct attribute_group ade7753_attribute_group = {
static const struct iio_info ade7753_info = {
.attrs = &ade7753_attribute_group,
+ .read_raw = &ade7753_read_raw,
+ .write_raw = &ade7753_write_raw,
.driver_module = THIS_MODULE,
};
--
1.8.3.1
next reply other threads:[~2016-09-29 16:01 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-29 16:01 Sandhya Bankar [this message]
[not found] ` <737de70d-80e5-a9b5-4522-8b66e6cd8ca6@kernel.org>
2016-10-01 22:29 ` [PATCH v2] Staging: iio: meter: ade7753: Replace IIO_DEV_ATTR_SAMP_FREQ attribute with IIO_CHAN_INFO_SAMP_FREQ handlers sandhya bankar
2016-10-03 20:25 ` 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=20160929160133.GA5460@localhost.localdomain \
--to=bankarsandhya512@gmail.com \
--cc=Michael.Hennerich@analog.com \
--cc=gregkh@linuxfoundation.org \
--cc=jic23@kernel.org \
--cc=knaack.h@gmx.de \
--cc=lars@metafoo.de \
--cc=outreachy-kernel@googlegroups.com \
--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.