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

staging: iio: ad7192: implement IIO_CHAN_INFO_SAMP_FREQ

This driver predates the availability of IIO_CHAN_INFO_SAMP_FREQ
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_SAMP_FREQ to implement the
sampling_frequency attribute instead of using IIO_DEV_ATTR_SAMP_FREQ()
macro.

Move code from the functions associated with IIO_DEV_ATTR_SAMP_FREQ()
into respective read and write hooks with the mask set to
IIO_CHAN_INFO_SAMP_FREQ.
Signed-off-by: default avatarEva Rachel Retuya <eraretuya@gmail.com>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent 598893e9
...@@ -322,57 +322,6 @@ static int ad7192_setup(struct ad7192_state *st, ...@@ -322,57 +322,6 @@ static int ad7192_setup(struct ad7192_state *st,
return ret; return ret;
} }
static ssize_t ad7192_read_frequency(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
struct ad7192_state *st = iio_priv(indio_dev);
return sprintf(buf, "%d\n", st->mclk /
(st->f_order * 1024 * AD7192_MODE_RATE(st->mode)));
}
static ssize_t ad7192_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 ad7192_state *st = iio_priv(indio_dev);
unsigned long lval;
int div, ret;
ret = kstrtoul(buf, 10, &lval);
if (ret)
return ret;
if (lval == 0)
return -EINVAL;
ret = iio_device_claim_direct_mode(indio_dev);
if (ret)
return ret;
div = st->mclk / (lval * st->f_order * 1024);
if (div < 1 || div > 1023) {
ret = -EINVAL;
goto out;
}
st->mode &= ~AD7192_MODE_RATE(-1);
st->mode |= AD7192_MODE_RATE(div);
ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
out:
iio_device_release_direct_mode(indio_dev);
return ret ? ret : len;
}
static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
ad7192_read_frequency,
ad7192_write_frequency);
static ssize_t static ssize_t
ad7192_show_scale_available(struct device *dev, ad7192_show_scale_available(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
...@@ -471,7 +420,6 @@ static IIO_DEVICE_ATTR(ac_excitation_en, S_IRUGO | S_IWUSR, ...@@ -471,7 +420,6 @@ static IIO_DEVICE_ATTR(ac_excitation_en, S_IRUGO | S_IWUSR,
AD7192_REG_MODE); AD7192_REG_MODE);
static struct attribute *ad7192_attributes[] = { static struct attribute *ad7192_attributes[] = {
&iio_dev_attr_sampling_frequency.dev_attr.attr,
&iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr, &iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,
&iio_dev_attr_in_voltage_scale_available.dev_attr.attr, &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
&iio_dev_attr_bridge_switch_en.dev_attr.attr, &iio_dev_attr_bridge_switch_en.dev_attr.attr,
...@@ -484,7 +432,6 @@ static const struct attribute_group ad7192_attribute_group = { ...@@ -484,7 +432,6 @@ static const struct attribute_group ad7192_attribute_group = {
}; };
static struct attribute *ad7195_attributes[] = { static struct attribute *ad7195_attributes[] = {
&iio_dev_attr_sampling_frequency.dev_attr.attr,
&iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr, &iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,
&iio_dev_attr_in_voltage_scale_available.dev_attr.attr, &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
&iio_dev_attr_bridge_switch_en.dev_attr.attr, &iio_dev_attr_bridge_switch_en.dev_attr.attr,
...@@ -536,6 +483,10 @@ static int ad7192_read_raw(struct iio_dev *indio_dev, ...@@ -536,6 +483,10 @@ static int ad7192_read_raw(struct iio_dev *indio_dev,
if (chan->type == IIO_TEMP) if (chan->type == IIO_TEMP)
*val -= 273 * ad7192_get_temp_scale(unipolar); *val -= 273 * ad7192_get_temp_scale(unipolar);
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_SAMP_FREQ:
*val = st->mclk /
(st->f_order * 1024 * AD7192_MODE_RATE(st->mode));
return IIO_VAL_INT;
} }
return -EINVAL; return -EINVAL;
...@@ -548,7 +499,7 @@ static int ad7192_write_raw(struct iio_dev *indio_dev, ...@@ -548,7 +499,7 @@ static int ad7192_write_raw(struct iio_dev *indio_dev,
long mask) long mask)
{ {
struct ad7192_state *st = iio_priv(indio_dev); struct ad7192_state *st = iio_priv(indio_dev);
int ret, i; int ret, i, div;
unsigned int tmp; unsigned int tmp;
ret = iio_device_claim_direct_mode(indio_dev); ret = iio_device_claim_direct_mode(indio_dev);
...@@ -572,6 +523,22 @@ static int ad7192_write_raw(struct iio_dev *indio_dev, ...@@ -572,6 +523,22 @@ static int ad7192_write_raw(struct iio_dev *indio_dev,
break; break;
} }
break; break;
case IIO_CHAN_INFO_SAMP_FREQ:
if (!val) {
ret = -EINVAL;
break;
}
div = st->mclk / (val * st->f_order * 1024);
if (div < 1 || div > 1023) {
ret = -EINVAL;
break;
}
st->mode &= ~AD7192_MODE_RATE(-1);
st->mode |= AD7192_MODE_RATE(div);
ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
break;
default: default:
ret = -EINVAL; ret = -EINVAL;
} }
...@@ -585,7 +552,14 @@ static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev, ...@@ -585,7 +552,14 @@ static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, struct iio_chan_spec const *chan,
long mask) long mask)
{ {
switch (mask) {
case IIO_CHAN_INFO_SCALE:
return IIO_VAL_INT_PLUS_NANO; return IIO_VAL_INT_PLUS_NANO;
case IIO_CHAN_INFO_SAMP_FREQ:
return IIO_VAL_INT;
default:
return -EINVAL;
}
} }
static const struct iio_info ad7192_info = { static const struct iio_info ad7192_info = {
......
...@@ -136,6 +136,7 @@ int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig); ...@@ -136,6 +136,7 @@ int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig);
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_OFFSET), \ BIT(IIO_CHAN_INFO_OFFSET), \
.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_SAMP_FREQ), \
.scan_index = (_si), \ .scan_index = (_si), \
.scan_type = { \ .scan_type = { \
.sign = 'u', \ .sign = 'u', \
......
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