Skip to content

Commit a679a20

Browse files
committed
iio: adc: ad7944: always use 32-bit DMA word for offload
The HDL team is trying to standardize on using always 32-bit DMA words for SPI offload. Add a new channel_offloads channel configuration to reflect this. Also, since we are adding a new channel configuration, we can move the sampling frequency attribute implementation to use IIO_CHAN_INFO_SAMP_FREQ. Signed-off-by: David Lechner <[email protected]>
1 parent ff26ad5 commit a679a20

File tree

1 file changed

+64
-76
lines changed

1 file changed

+64
-76
lines changed

drivers/iio/adc/ad7944.c

+64-76
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ struct ad7944_chip_info {
120120
const char *name;
121121
const struct ad7944_timing_spec *timing_spec;
122122
const struct iio_chan_spec channels[2];
123+
const struct iio_chan_spec offload_channels[1];
123124
};
124125

125126
/*
@@ -150,6 +151,23 @@ static const struct ad7944_chip_info _name##_chip_info = { \
150151
}, \
151152
IIO_CHAN_SOFT_TIMESTAMP(1), \
152153
}, \
154+
.offload_channels = { \
155+
{ \
156+
.type = IIO_VOLTAGE, \
157+
.indexed = 1, \
158+
.differential = _diff, \
159+
.channel = 0, \
160+
.channel2 = _diff ? 1 : 0, \
161+
.scan_index = 0, \
162+
.scan_type.sign = _diff ? 's' : 'u', \
163+
.scan_type.realbits = _bits, \
164+
.scan_type.storagebits = 32, \
165+
.scan_type.endianness = IIO_CPU, \
166+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
167+
| BIT(IIO_CHAN_INFO_SCALE) \
168+
| BIT(IIO_CHAN_INFO_SAMP_FREQ), \
169+
}, \
170+
}, \
153171
}
154172

155173
/* pseudo-differential with ground sense */
@@ -194,7 +212,7 @@ static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *
194212

195213
/* Then we can read the data during the acquisition phase */
196214
xfers[2].rx_buf = &adc->sample.raw;
197-
xfers[2].len = BITS_TO_BYTES(chan->scan_type.storagebits);
215+
xfers[2].len = chan->scan_type.realbits > 16 ? 4 : 2;
198216
xfers[2].bits_per_word = chan->scan_type.realbits;
199217

200218
spi_message_init_with_transfers(&adc->msg, xfers, 3);
@@ -228,7 +246,7 @@ static int ad7944_3wire_cs_mode_init_turbo_msg(struct device *dev,
228246

229247
/* read sample data from previous conversion */
230248
xfers[1].rx_buf = &adc->sample.raw;
231-
xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
249+
xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;
232250
xfers[1].bits_per_word = chan->scan_type.realbits;
233251
/*
234252
* CNV has to be high at end of conversion to avoid triggering the busy
@@ -279,7 +297,7 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
279297
xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
280298

281299
xfers[1].rx_buf = &adc->sample.raw;
282-
xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
300+
xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;
283301
xfers[1].bits_per_word = chan->scan_type.realbits;
284302

285303
spi_message_init_with_transfers(&adc->msg, xfers, 3);
@@ -356,7 +374,7 @@ static int ad7944_single_conversion(struct ad7944_adc *adc,
356374
return -EOPNOTSUPP;
357375
}
358376

359-
if (chan->scan_type.storagebits > 16)
377+
if (chan->scan_type.realbits > 16)
360378
*val = adc->sample.raw.u32;
361379
else
362380
*val = adc->sample.raw.u16;
@@ -399,90 +417,60 @@ static int ad7944_read_raw(struct iio_dev *indio_dev,
399417
return -EINVAL;
400418
}
401419

420+
case IIO_CHAN_INFO_SAMP_FREQ: {
421+
/* TODO: get actual hw period */
422+
u64 period_ns = pwm_get_period(adc->pwm);
423+
424+
*val = DIV_ROUND_UP_ULL(NSEC_PER_SEC, period_ns);
425+
return IIO_VAL_INT;
426+
}
427+
402428
default:
403429
return -EINVAL;
404430
}
405431
}
406432

407-
static ssize_t ad7944_sampling_frequency_show(struct device *dev,
408-
struct device_attribute *attr,
409-
char *buf)
433+
static int ad7944_write_raw(struct iio_dev *indio_dev,
434+
const struct iio_chan_spec *chan,
435+
int val, int val2, long info)
410436
{
411-
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
412437
struct ad7944_adc *adc = iio_priv(indio_dev);
413-
u64 period_ns;
414-
415-
if (!adc->pwm)
416-
return -ENODEV;
417-
418-
period_ns = pwm_get_period(adc->pwm);
419438

420-
return sysfs_emit(buf, "%llu\n", div64_u64(NSEC_PER_SEC, period_ns));
421-
}
422-
423-
static ssize_t ad7944_sampling_frequency_store(struct device *dev,
424-
struct device_attribute *attr,
425-
const char *buf, size_t len)
426-
{
427-
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
428-
struct ad7944_adc *adc = iio_priv(indio_dev);
429-
struct pwm_state sample_state;
430-
u32 val;
431-
int ret;
439+
switch (info) {
440+
case IIO_CHAN_INFO_SAMP_FREQ: {
441+
struct pwm_state state;
432442

433-
if (!adc->pwm)
434-
return -ENODEV;
443+
if (val < 0)
444+
return -EINVAL;
435445

436-
ret = kstrtouint(buf, 0, &val);
437-
if (ret)
438-
return ret;
446+
pwm_init_state(adc->pwm, &state);
447+
state.period = DIV_ROUND_UP(NSEC_PER_SEC, val);
448+
state.duty_cycle = AD7944_PWM_TRIGGER_DUTY_CYCLE_NS;
449+
state.enabled = true;
439450

440-
if (val == 0)
451+
return pwm_apply_state(adc->pwm, &state);
452+
}
453+
default:
441454
return -EINVAL;
442-
443-
pwm_init_state(adc->pwm, &sample_state);
444-
sample_state.period = div_u64(NSEC_PER_SEC, val);
445-
sample_state.duty_cycle = AD7944_PWM_TRIGGER_DUTY_CYCLE_NS;
446-
sample_state.enabled = true;
447-
448-
ret = pwm_apply_state(adc->pwm, &sample_state);
449-
if (ret)
450-
return ret;
451-
452-
return len;
455+
}
453456
}
454457

455-
static IIO_DEV_ATTR_SAMP_FREQ(0644, ad7944_sampling_frequency_show,
456-
ad7944_sampling_frequency_store);
457-
458-
static struct attribute *ad7944_attrs[] = {
459-
&iio_dev_attr_sampling_frequency.dev_attr.attr,
460-
NULL
461-
};
462-
463-
static umode_t ad7944_attrs_is_visible(struct kobject *kobj,
464-
struct attribute *attr, int unused)
458+
static int ad7944_write_raw_get_fmt(struct iio_dev *indio_dev,
459+
const struct iio_chan_spec *chan,
460+
long info)
465461
{
466-
struct device *dev = kobj_to_dev(kobj);
467-
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
468-
struct ad7944_adc *adc = iio_priv(indio_dev);
469-
470-
/* hide sampling_frequency attribute when there is no pwm */
471-
if (attr == &iio_dev_attr_sampling_frequency.dev_attr.attr && !adc->pwm)
472-
return 0;
473-
474-
/* show all other attributes */
475-
return attr->mode;
462+
switch (info) {
463+
case IIO_CHAN_INFO_SAMP_FREQ:
464+
return IIO_VAL_INT;
465+
default:
466+
return IIO_VAL_INT_PLUS_MICRO;
467+
}
476468
}
477469

478-
static const struct attribute_group ad7944_group = {
479-
.attrs = ad7944_attrs,
480-
.is_visible = ad7944_attrs_is_visible,
481-
};
482-
483470
static const struct iio_info ad7944_iio_info = {
484471
.read_raw = &ad7944_read_raw,
485-
.attrs = &ad7944_group,
472+
.write_raw = &ad7944_write_raw,
473+
.write_raw_get_fmt = &ad7944_write_raw_get_fmt,
486474
};
487475

488476
static int ad7944_offload_ex_buffer_preenable(struct iio_dev *indio_dev)
@@ -730,8 +718,6 @@ static int ad7944_probe(struct spi_device *spi)
730718
indio_dev->name = chip_info->name;
731719
indio_dev->modes = INDIO_DIRECT_MODE;
732720
indio_dev->info = &ad7944_iio_info;
733-
indio_dev->channels = chip_info->channels;
734-
indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
735721

736722
if (spi_engine_ex_offload_supported(spi)) {
737723
struct pwm_state state = {
@@ -740,6 +726,10 @@ static int ad7944_probe(struct spi_device *spi)
740726
.enabled = true,
741727
};
742728

729+
indio_dev->channels = chip_info->offload_channels;
730+
indio_dev->num_channels = ARRAY_SIZE(chip_info->offload_channels);
731+
indio_dev->setup_ops = &ad7944_offload_ex_buffer_setup_ops;
732+
743733
adc->pwm = devm_pwm_get(dev, NULL);
744734
if (IS_ERR(adc->pwm))
745735
return dev_err_probe(dev, PTR_ERR(adc->pwm),
@@ -754,12 +744,10 @@ static int ad7944_probe(struct spi_device *spi)
754744
IIO_BUFFER_DIRECTION_IN);
755745
if (ret)
756746
return ret;
757-
758-
indio_dev->setup_ops = &ad7944_offload_ex_buffer_setup_ops;
759-
760-
/* can't have soft timestamp with SPI offload */
761-
indio_dev->num_channels--;
762747
} else {
748+
indio_dev->channels = chip_info->channels;
749+
indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
750+
763751
ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
764752
iio_pollfunc_store_time,
765753
ad7944_trigger_handler,

0 commit comments

Comments
 (0)