Commit 4a5936da authored by Eva Rachel Retuya's avatar Eva Rachel Retuya Committed by Jonathan Cameron

staging: iio: ad7606: implement IIO_CHAN_INFO_OVERSAMPLING_RATIO

This driver predates the availability of IIO_CHAN_INFO_OVERSAMPLING_RATIO
attribute wherein usage has some advantages like it can be accessed by
in-kernel consumers as well as reduces the code size.

Therefore, use IIO_CHAN_INFO_OVERSAMPLING_RATIO to implement the
oversampling_ratio attribute instead of using IIO_DEVICE_ATTR() macro.

Move code from the functions associated with IIO_DEVICE_ATTR() into
the read_raw hook as well as add the write_raw hook with both masks set
to IIO_CHAN_INFO_OVERSAMPLING_RATIO.
Signed-off-by: default avatarEva Rachel Retuya <eraretuya@gmail.com>
Acked-by: default avatarLars-Peter Clausen <lars@metafoo.de>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent b321a38d
...@@ -103,6 +103,9 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, ...@@ -103,6 +103,9 @@ static int ad7606_read_raw(struct iio_dev *indio_dev,
*val = st->range * 2; *val = st->range * 2;
*val2 = st->chip_info->channels[0].scan_type.realbits; *val2 = st->chip_info->channels[0].scan_type.realbits;
return IIO_VAL_FRACTIONAL_LOG2; return IIO_VAL_FRACTIONAL_LOG2;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
*val = st->oversampling;
return IIO_VAL_INT;
} }
return -EINVAL; return -EINVAL;
} }
...@@ -145,16 +148,6 @@ static IIO_DEVICE_ATTR(in_voltage_range, S_IRUGO | S_IWUSR, ...@@ -145,16 +148,6 @@ static IIO_DEVICE_ATTR(in_voltage_range, S_IRUGO | S_IWUSR,
ad7606_show_range, ad7606_store_range, 0); ad7606_show_range, ad7606_store_range, 0);
static IIO_CONST_ATTR(in_voltage_range_available, "5000 10000"); static IIO_CONST_ATTR(in_voltage_range_available, "5000 10000");
static ssize_t ad7606_show_oversampling_ratio(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
struct ad7606_state *st = iio_priv(indio_dev);
return sprintf(buf, "%u\n", st->oversampling);
}
static int ad7606_oversampling_get_index(unsigned int val) static int ad7606_oversampling_get_index(unsigned int val)
{ {
unsigned char supported[] = {0, 2, 4, 8, 16, 32, 64}; unsigned char supported[] = {0, 2, 4, 8, 16, 32, 64};
...@@ -167,44 +160,43 @@ static int ad7606_oversampling_get_index(unsigned int val) ...@@ -167,44 +160,43 @@ static int ad7606_oversampling_get_index(unsigned int val)
return -EINVAL; return -EINVAL;
} }
static ssize_t ad7606_store_oversampling_ratio(struct device *dev, static int ad7606_write_raw(struct iio_dev *indio_dev,
struct device_attribute *attr, struct iio_chan_spec const *chan,
const char *buf, size_t count) int val,
int val2,
long mask)
{ {
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
struct ad7606_state *st = iio_priv(indio_dev); struct ad7606_state *st = iio_priv(indio_dev);
unsigned long lval;
int ret; int ret;
ret = kstrtoul(buf, 10, &lval); switch (mask) {
if (ret) case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
return ret; if (val2)
return -EINVAL;
ret = ad7606_oversampling_get_index(val);
if (ret < 0) {
dev_err(st->dev, "oversampling %d is not supported\n",
val);
return ret;
}
ret = ad7606_oversampling_get_index(lval); mutex_lock(&indio_dev->mlock);
if (ret < 0) { gpio_set_value(st->pdata->gpio_os0, (ret >> 0) & 1);
dev_err(dev, "oversampling %lu is not supported\n", lval); gpio_set_value(st->pdata->gpio_os1, (ret >> 1) & 1);
return ret; gpio_set_value(st->pdata->gpio_os2, (ret >> 2) & 1);
st->oversampling = val;
mutex_unlock(&indio_dev->mlock);
return 0;
default:
return -EINVAL;
} }
mutex_lock(&indio_dev->mlock);
gpio_set_value(st->pdata->gpio_os0, (ret >> 0) & 1);
gpio_set_value(st->pdata->gpio_os1, (ret >> 1) & 1);
gpio_set_value(st->pdata->gpio_os2, (ret >> 2) & 1);
st->oversampling = lval;
mutex_unlock(&indio_dev->mlock);
return count;
} }
static IIO_DEVICE_ATTR(oversampling_ratio, S_IRUGO | S_IWUSR,
ad7606_show_oversampling_ratio,
ad7606_store_oversampling_ratio, 0);
static IIO_CONST_ATTR(oversampling_ratio_available, "0 2 4 8 16 32 64"); static IIO_CONST_ATTR(oversampling_ratio_available, "0 2 4 8 16 32 64");
static struct attribute *ad7606_attributes_os_and_range[] = { static struct attribute *ad7606_attributes_os_and_range[] = {
&iio_dev_attr_in_voltage_range.dev_attr.attr, &iio_dev_attr_in_voltage_range.dev_attr.attr,
&iio_const_attr_in_voltage_range_available.dev_attr.attr, &iio_const_attr_in_voltage_range_available.dev_attr.attr,
&iio_dev_attr_oversampling_ratio.dev_attr.attr,
&iio_const_attr_oversampling_ratio_available.dev_attr.attr, &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
NULL, NULL,
}; };
...@@ -214,7 +206,6 @@ static const struct attribute_group ad7606_attribute_group_os_and_range = { ...@@ -214,7 +206,6 @@ static const struct attribute_group ad7606_attribute_group_os_and_range = {
}; };
static struct attribute *ad7606_attributes_os[] = { static struct attribute *ad7606_attributes_os[] = {
&iio_dev_attr_oversampling_ratio.dev_attr.attr,
&iio_const_attr_oversampling_ratio_available.dev_attr.attr, &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
NULL, NULL,
}; };
...@@ -241,6 +232,8 @@ static const struct attribute_group ad7606_attribute_group_range = { ...@@ -241,6 +232,8 @@ static const struct attribute_group ad7606_attribute_group_range = {
.address = num, \ .address = num, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
.info_mask_shared_by_all = \
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
.scan_index = num, \ .scan_index = num, \
.scan_type = { \ .scan_type = { \
.sign = 's', \ .sign = 's', \
...@@ -429,12 +422,14 @@ static const struct iio_info ad7606_info_no_os_or_range = { ...@@ -429,12 +422,14 @@ static const struct iio_info ad7606_info_no_os_or_range = {
static const struct iio_info ad7606_info_os_and_range = { static const struct iio_info ad7606_info_os_and_range = {
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
.read_raw = &ad7606_read_raw, .read_raw = &ad7606_read_raw,
.write_raw = &ad7606_write_raw,
.attrs = &ad7606_attribute_group_os_and_range, .attrs = &ad7606_attribute_group_os_and_range,
}; };
static const struct iio_info ad7606_info_os = { static const struct iio_info ad7606_info_os = {
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
.read_raw = &ad7606_read_raw, .read_raw = &ad7606_read_raw,
.write_raw = &ad7606_write_raw,
.attrs = &ad7606_attribute_group_os, .attrs = &ad7606_attribute_group_os,
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment