Skip to content

Commit 381522c

Browse files
commodoJonathan Cameron
authored andcommitted
iio: adc: ad7791: implement IIO_CHAN_INFO_SAMP_FREQ
Now that the old read/write frequency sysfs attrs have been removed, we have a clean slate to implement IIO_CHAN_INFO_SAMP_FREQ. This driver also pre-dates IIO_CHAN_INFO_SAMP_FREQ, and this change implements this behavior. The `ad7791_write_raw` would have overlapped quite a bit with the old read/write frequency functions, making things a bit harder to follow. Fixes: a13e831 ("staging: iio: ad7192: implement IIO_CHAN_INFO_SAMP_FREQ") Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 7eb6b35 commit 381522c

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

‎drivers/iio/adc/ad7791.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ struct ad7791_state {
153153
const struct ad7791_chip_info *info;
154154
};
155155

156+
static const int ad7791_sample_freq_avail[8][2] = {
157+
[AD7791_FILTER_RATE_120] = { 120, 0 },
158+
[AD7791_FILTER_RATE_100] = { 100, 0 },
159+
[AD7791_FILTER_RATE_33_3] = { 33, 300000 },
160+
[AD7791_FILTER_RATE_20] = { 20, 0 },
161+
[AD7791_FILTER_RATE_16_6] = { 16, 600000 },
162+
[AD7791_FILTER_RATE_16_7] = { 16, 700000 },
163+
[AD7791_FILTER_RATE_13_3] = { 13, 300000 },
164+
[AD7791_FILTER_RATE_9_5] = { 9, 500000 },
165+
};
166+
156167
static struct ad7791_state *ad_sigma_delta_to_ad7791(struct ad_sigma_delta *sd)
157168
{
158169
return container_of(sd, struct ad7791_state, sd);
@@ -202,6 +213,7 @@ static int ad7791_read_raw(struct iio_dev *indio_dev,
202213
{
203214
struct ad7791_state *st = iio_priv(indio_dev);
204215
bool unipolar = !!(st->mode & AD7791_MODE_UNIPOLAR);
216+
unsigned int rate;
205217

206218
switch (info) {
207219
case IIO_CHAN_INFO_RAW:
@@ -239,11 +251,53 @@ static int ad7791_read_raw(struct iio_dev *indio_dev,
239251
*val2 = chan->scan_type.realbits - 1;
240252

241253
return IIO_VAL_FRACTIONAL_LOG2;
254+
case IIO_CHAN_INFO_SAMP_FREQ:
255+
rate = st->filter & AD7791_FILTER_RATE_MASK;
256+
*val = ad7791_sample_freq_avail[rate][0];
257+
*val2 = ad7791_sample_freq_avail[rate][1];
258+
return IIO_VAL_INT_PLUS_MICRO;
242259
}
243260

244261
return -EINVAL;
245262
}
246263

264+
static int ad7791_write_raw(struct iio_dev *indio_dev,
265+
struct iio_chan_spec const *chan, int val, int val2, long mask)
266+
{
267+
struct ad7791_state *st = iio_priv(indio_dev);
268+
int ret, i;
269+
270+
ret = iio_device_claim_direct_mode(indio_dev);
271+
if (ret)
272+
return ret;
273+
274+
switch (mask) {
275+
case IIO_CHAN_INFO_SAMP_FREQ:
276+
for (i = 0; i < ARRAY_SIZE(ad7791_sample_freq_avail); i++) {
277+
if (ad7791_sample_freq_avail[i][0] == val &&
278+
ad7791_sample_freq_avail[i][1] == val2)
279+
break;
280+
}
281+
282+
if (i == ARRAY_SIZE(ad7791_sample_freq_avail)) {
283+
ret = -EINVAL;
284+
break;
285+
}
286+
287+
st->filter &= ~AD7791_FILTER_RATE_MASK;
288+
st->filter |= i;
289+
ad_sd_write_reg(&st->sd, AD7791_REG_FILTER,
290+
sizeof(st->filter),
291+
st->filter);
292+
break;
293+
default:
294+
ret = -EINVAL;
295+
}
296+
297+
iio_device_release_direct_mode(indio_dev);
298+
return ret;
299+
}
300+
247301
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("120 100 33.3 20 16.7 16.6 13.3 9.5");
248302

249303
static struct attribute *ad7791_attributes[] = {
@@ -257,12 +311,14 @@ static const struct attribute_group ad7791_attribute_group = {
257311

258312
static const struct iio_info ad7791_info = {
259313
.read_raw = &ad7791_read_raw,
314+
.write_raw = &ad7791_write_raw,
260315
.attrs = &ad7791_attribute_group,
261316
.validate_trigger = ad_sd_validate_trigger,
262317
};
263318

264319
static const struct iio_info ad7791_no_filter_info = {
265320
.read_raw = &ad7791_read_raw,
321+
.write_raw = &ad7791_write_raw,
266322
.validate_trigger = ad_sd_validate_trigger,
267323
};
268324

0 commit comments

Comments
 (0)