Skip to content

Commit a13e831

Browse files
eraretuyajic23
authored andcommitted
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: Eva Rachel Retuya <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 598893e commit a13e831

File tree

2 files changed

+30
-55
lines changed

2 files changed

+30
-55
lines changed

drivers/staging/iio/adc/ad7192.c

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -322,57 +322,6 @@ static int ad7192_setup(struct ad7192_state *st,
322322
return ret;
323323
}
324324

325-
static ssize_t ad7192_read_frequency(struct device *dev,
326-
struct device_attribute *attr,
327-
char *buf)
328-
{
329-
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
330-
struct ad7192_state *st = iio_priv(indio_dev);
331-
332-
return sprintf(buf, "%d\n", st->mclk /
333-
(st->f_order * 1024 * AD7192_MODE_RATE(st->mode)));
334-
}
335-
336-
static ssize_t ad7192_write_frequency(struct device *dev,
337-
struct device_attribute *attr,
338-
const char *buf,
339-
size_t len)
340-
{
341-
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
342-
struct ad7192_state *st = iio_priv(indio_dev);
343-
unsigned long lval;
344-
int div, ret;
345-
346-
ret = kstrtoul(buf, 10, &lval);
347-
if (ret)
348-
return ret;
349-
if (lval == 0)
350-
return -EINVAL;
351-
352-
ret = iio_device_claim_direct_mode(indio_dev);
353-
if (ret)
354-
return ret;
355-
356-
div = st->mclk / (lval * st->f_order * 1024);
357-
if (div < 1 || div > 1023) {
358-
ret = -EINVAL;
359-
goto out;
360-
}
361-
362-
st->mode &= ~AD7192_MODE_RATE(-1);
363-
st->mode |= AD7192_MODE_RATE(div);
364-
ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
365-
366-
out:
367-
iio_device_release_direct_mode(indio_dev);
368-
369-
return ret ? ret : len;
370-
}
371-
372-
static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
373-
ad7192_read_frequency,
374-
ad7192_write_frequency);
375-
376325
static ssize_t
377326
ad7192_show_scale_available(struct device *dev,
378327
struct device_attribute *attr, char *buf)
@@ -471,7 +420,6 @@ static IIO_DEVICE_ATTR(ac_excitation_en, S_IRUGO | S_IWUSR,
471420
AD7192_REG_MODE);
472421

473422
static struct attribute *ad7192_attributes[] = {
474-
&iio_dev_attr_sampling_frequency.dev_attr.attr,
475423
&iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,
476424
&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
477425
&iio_dev_attr_bridge_switch_en.dev_attr.attr,
@@ -484,7 +432,6 @@ static const struct attribute_group ad7192_attribute_group = {
484432
};
485433

486434
static struct attribute *ad7195_attributes[] = {
487-
&iio_dev_attr_sampling_frequency.dev_attr.attr,
488435
&iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,
489436
&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
490437
&iio_dev_attr_bridge_switch_en.dev_attr.attr,
@@ -536,6 +483,10 @@ static int ad7192_read_raw(struct iio_dev *indio_dev,
536483
if (chan->type == IIO_TEMP)
537484
*val -= 273 * ad7192_get_temp_scale(unipolar);
538485
return IIO_VAL_INT;
486+
case IIO_CHAN_INFO_SAMP_FREQ:
487+
*val = st->mclk /
488+
(st->f_order * 1024 * AD7192_MODE_RATE(st->mode));
489+
return IIO_VAL_INT;
539490
}
540491

541492
return -EINVAL;
@@ -548,7 +499,7 @@ static int ad7192_write_raw(struct iio_dev *indio_dev,
548499
long mask)
549500
{
550501
struct ad7192_state *st = iio_priv(indio_dev);
551-
int ret, i;
502+
int ret, i, div;
552503
unsigned int tmp;
553504

554505
ret = iio_device_claim_direct_mode(indio_dev);
@@ -572,6 +523,22 @@ static int ad7192_write_raw(struct iio_dev *indio_dev,
572523
break;
573524
}
574525
break;
526+
case IIO_CHAN_INFO_SAMP_FREQ:
527+
if (!val) {
528+
ret = -EINVAL;
529+
break;
530+
}
531+
532+
div = st->mclk / (val * st->f_order * 1024);
533+
if (div < 1 || div > 1023) {
534+
ret = -EINVAL;
535+
break;
536+
}
537+
538+
st->mode &= ~AD7192_MODE_RATE(-1);
539+
st->mode |= AD7192_MODE_RATE(div);
540+
ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
541+
break;
575542
default:
576543
ret = -EINVAL;
577544
}
@@ -585,7 +552,14 @@ static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev,
585552
struct iio_chan_spec const *chan,
586553
long mask)
587554
{
588-
return IIO_VAL_INT_PLUS_NANO;
555+
switch (mask) {
556+
case IIO_CHAN_INFO_SCALE:
557+
return IIO_VAL_INT_PLUS_NANO;
558+
case IIO_CHAN_INFO_SAMP_FREQ:
559+
return IIO_VAL_INT;
560+
default:
561+
return -EINVAL;
562+
}
589563
}
590564

591565
static const struct iio_info ad7192_info = {

include/linux/iio/adc/ad_sigma_delta.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig);
136136
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
137137
BIT(IIO_CHAN_INFO_OFFSET), \
138138
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
139+
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
139140
.scan_index = (_si), \
140141
.scan_type = { \
141142
.sign = 'u', \

0 commit comments

Comments
 (0)