/* * When SPI offload is configured, transfers are executed without CPU * intervention so no soft timestamp can be recorded when transfers run. * Because of that, the macros that set timestamp channel are only used when * transfers are not offloaded.
*/ #define AD4000_DIFF_CHANNELS(_sign, _real_bits, _reg_access) \
{ \
AD4000_DIFF_CHANNEL(_sign, _real_bits, _reg_access, 0), \
IIO_CHAN_SOFT_TIMESTAMP(1), \
}
/* * DMA (thus cache coherency maintenance) requires the transfer buffers * to live in their own cache lines.
*/ struct { union {
__be16 sample_buf16_be;
__be32 sample_buf32_be;
u16 sample_buf16;
u32 sample_buf32;
} data;
aligned_s64 timestamp;
} scan __aligned(IIO_DMA_MINALIGN);
u8 tx_buf[2];
u8 rx_buf[2];
};
staticvoid ad4000_fill_scale_tbl(struct ad4000_state *st, struct iio_chan_spec const *chan)
{ int val, tmp0, tmp1; int scale_bits;
u64 tmp2;
/* * ADCs that output two's complement code have one less bit to express * voltage magnitude.
*/ if (chan->scan_type.sign == 's')
scale_bits = chan->scan_type.realbits - 1; else
scale_bits = chan->scan_type.realbits;
/* * The gain is stored as a fraction of 1000 and, as we need to * divide vref_mv by the gain, we invert the gain/1000 fraction. * Also multiply by an extra MILLI to preserve precision. * Thus, we have MILLI * MILLI equals MICRO as fraction numerator.
*/
val = mult_frac(st->vref_mv, MICRO, st->gain_milli);
/* Would multiply by NANO here but we multiplied by extra MILLI */
tmp2 = (u64)val * MICRO >> scale_bits;
tmp0 = div_s64_rem(tmp2, NANO, &tmp1);
/* Store scale for when span compression is disabled */
st->scale_tbl[0][0] = tmp0; /* Integer part */
st->scale_tbl[0][1] = abs(tmp1); /* Fractional part */
/* Store scale for when span compression is enabled */
st->scale_tbl[1][0] = tmp0;
/* The integer part is always zero so don't bother to divide it. */ if (chan->differential)
st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 4, 5); else
st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 9, 10);
}
staticint ad4000_convert_and_acquire(struct ad4000_state *st)
{ int ret;
/* * In 4-wire mode, the CNV line is held high for the entire conversion * and acquisition process. In other modes, the CNV GPIO is optional * and, if provided, replaces controller CS. If CNV GPIO is not defined * gpiod_set_value_cansleep() has no effect.
*/
gpiod_set_value_cansleep(st->cnv_gpio, 1);
ret = spi_sync(st->spi, &st->msg);
gpiod_set_value_cansleep(st->cnv_gpio, 0);
return ret;
}
staticint ad4000_single_conversion(struct iio_dev *indio_dev, conststruct iio_chan_spec *chan, int *val)
{ struct ad4000_state *st = iio_priv(indio_dev);
u32 sample; int ret;
ret = ad4000_convert_and_acquire(st); if (ret < 0) return ret;
ret = ad4000_write_reg(st, reg_val); if (ret < 0) return ret;
st->span_comp = span_comp_en; return 0;
}
staticint ad4000_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask)
{ struct ad4000_state *st = iio_priv(indio_dev); int ret;
switch (mask) { case IIO_CHAN_INFO_SCALE: if (!iio_device_claim_direct(indio_dev)) return -EBUSY;
ret = __ad4000_write_raw(indio_dev, chan, val2);
iio_device_release_direct(indio_dev); return ret; case IIO_CHAN_INFO_SAMP_FREQ: if (val < 1 || val > st->max_rate_hz) return -EINVAL;
if (!iio_device_claim_direct(indio_dev)) return -EBUSY;
ret = ad4000_set_sampling_freq(st, val);
iio_device_release_direct(indio_dev); return ret; default: return -EINVAL;
}
}
st->offload_trigger = devm_spi_offload_trigger_get(dev, st->offload,
SPI_OFFLOAD_TRIGGER_PERIODIC); if (IS_ERR(st->offload_trigger)) return dev_err_probe(dev, PTR_ERR(st->offload_trigger), "Failed to get offload trigger\n");
ret = ad4000_set_sampling_freq(st, st->max_rate_hz); if (ret) return dev_err_probe(dev, ret, "Failed to set sampling frequency\n");
rx_dma = devm_spi_offload_rx_stream_request_dma_chan(dev, st->offload); if (IS_ERR(rx_dma)) return dev_err_probe(dev, PTR_ERR(rx_dma), "Failed to get offload RX DMA\n");
ret = devm_iio_dmaengine_buffer_setup_with_handle(dev, indio_dev, rx_dma,
IIO_BUFFER_DIRECTION_IN); if (ret) return dev_err_probe(dev, ret, "Failed to setup DMA buffer\n");
return 0;
}
/* * This executes a data sample transfer when using SPI offloading. The device * connections should be in "3-wire" mode, selected either when the adi,sdi-pin * device tree property is absent or set to "high". Also, the ADC CNV pin must * be connected to a SPI controller CS (it can't be connected to a GPIO). * * In order to achieve the maximum sample rate, we only do one transfer per * SPI offload trigger. Because the ADC output has a one sample latency (delay) * when the device is wired in "3-wire" mode and only one transfer per sample is * being made in turbo mode, the first data sample is not valid because it * contains the output of an earlier conversion result. We also set transfer * `bits_per_word` to achieve higher throughput by using the minimum number of * SCLK cycles. Also, a delay is added to make sure we meet the minimum quiet * time before releasing the CS line. * * Note that, with `bits_per_word` set to the number of ADC precision bits, * transfers use larger word sizes that get stored in 'in-memory wordsizes' that * are always in native CPU byte order. Because of that, IIO buffer elements * ought to be read in CPU endianness which requires setting IIO scan_type * endianness accordingly (i.e. IIO_CPU).
*/ staticint ad4000_prepare_offload_message(struct ad4000_state *st, conststruct iio_chan_spec *chan)
{ struct spi_transfer *xfer = &st->offload_xfer;
/* * This executes a data sample transfer for when the device connections are * in "3-wire" mode, selected when the adi,sdi-pin device tree property is * absent or set to "high". In this connection mode, the ADC SDI pin is * connected to MOSI or to VIO and ADC CNV pin is connected either to a SPI * controller CS or to a GPIO. * AD4000 series of devices initiate conversions on the rising edge of CNV pin. * * If the CNV pin is connected to an SPI controller CS line (which is by default * active low), the ADC readings would have a latency (delay) of one read. * Moreover, since we also do ADC sampling for filling the buffer on triggered * buffer mode, the timestamps of buffer readings would be disarranged. * To prevent the read latency and reduce the time discrepancy between the * sample read request and the time of actual sampling by the ADC, do a * preparatory transfer to pulse the CS/CNV line.
*/ staticint ad4000_prepare_3wire_mode_message(struct ad4000_state *st, conststruct iio_chan_spec *chan)
{ struct spi_transfer *xfers = st->xfers;
/* * If the device is set up for SPI offloading, IIO channel scan_type is * set to IIO_CPU. When that is the case, use larger SPI word sizes for * single-shot reads too. Thus, sample data can be correctly handled in * ad4000_single_conversion() according to scan_type endianness.
*/ if (chan->scan_type.endianness != IIO_BE)
xfers[1].bits_per_word = chan->scan_type.realbits;
xfers[1].delay.value = st->time_spec->t_quiet2_ns;
xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS;
/* * This executes a data sample transfer for when the device connections are * in "4-wire" mode, selected when the adi,sdi-pin device tree property is * set to "cs". In this connection mode, the controller CS pin is connected to * ADC SDI pin and a GPIO is connected to ADC CNV pin. * The GPIO connected to ADC CNV pin is set outside of the SPI transfer.
*/ staticint ad4000_prepare_4wire_mode_message(struct ad4000_state *st, conststruct iio_chan_spec *chan)
{ struct spi_transfer *xfers = st->xfers;
/* * Dummy transfer to cause enough delay between CNV going high and SDI * going low.
*/
xfers[0].cs_off = 1;
xfers[0].delay.value = st->time_spec->t_conv_ns;
xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad4000_power_supplies),
ad4000_power_supplies); if (ret) return dev_err_probe(dev, ret, "Failed to enable power supplies\n");
ret = devm_regulator_get_enable_read_voltage(dev, "ref"); if (ret < 0) return dev_err_probe(dev, ret, "Failed to get ref regulator reference\n");
st->vref_mv = ret / 1000;
st->cnv_gpio = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_HIGH); if (IS_ERR(st->cnv_gpio)) return dev_err_probe(dev, PTR_ERR(st->cnv_gpio), "Failed to get CNV GPIO");
st->offload = devm_spi_offload_get(dev, spi, &ad4000_offload_config);
ret = PTR_ERR_OR_ZERO(st->offload); if (ret && ret != -ENODEV) return dev_err_probe(dev, ret, "Failed to get offload\n");
st->using_offload = !IS_ERR(st->offload); if (st->using_offload) {
indio_dev->setup_ops = &ad4000_offload_buffer_setup_ops;
ret = ad4000_spi_offload_setup(indio_dev, st); if (ret) return ret;
} else {
ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
&iio_pollfunc_store_time,
&ad4000_trigger_handler,
NULL); if (ret) return ret;
}
ret = device_property_match_property_string(dev, "adi,sdi-pin",
ad4000_sdi_pin,
ARRAY_SIZE(ad4000_sdi_pin)); if (ret < 0 && ret != -EINVAL) return dev_err_probe(dev, ret, "getting adi,sdi-pin property failed\n");
/* Default to usual SPI connections if pin properties are not present */
st->sdi_pin = ret == -EINVAL ? AD4000_SDI_MOSI : ret; switch (st->sdi_pin) { case AD4000_SDI_MOSI:
indio_dev->info = &ad4000_reg_access_info;
/* * In "3-wire mode", the ADC SDI line must be kept high when * data is not being clocked out of the controller. * Request the SPI controller to make MOSI idle high.
*/
spi->mode |= SPI_MOSI_IDLE_HIGH;
ret = spi_setup(spi); if (ret < 0) return ret;
if (st->using_offload) {
indio_dev->channels = &chip->reg_access_offload_chan_spec;
indio_dev->num_channels = 1;
ret = ad4000_prepare_offload_message(st, indio_dev->channels); if (ret) return dev_err_probe(dev, ret, "Failed to optimize SPI msg\n");
} else {
indio_dev->channels = chip->reg_access_chan_spec;
indio_dev->num_channels = ARRAY_SIZE(chip->reg_access_chan_spec);
}
/* * Call ad4000_prepare_3wire_mode_message() so single-shot read * SPI messages are always initialized.
*/
ret = ad4000_prepare_3wire_mode_message(st, &indio_dev->channels[0]); if (ret) return dev_err_probe(dev, ret, "Failed to optimize SPI msg\n");
ret = ad4000_config(st); if (ret < 0) return dev_err_probe(dev, ret, "Failed to config device\n");
break; case AD4000_SDI_VIO: if (st->using_offload) {
indio_dev->info = &ad4000_offload_info;
indio_dev->channels = &chip->offload_chan_spec;
indio_dev->num_channels = 1;
ret = ad4000_prepare_offload_message(st, indio_dev->channels); if (ret) return dev_err_probe(dev, ret, "Failed to optimize SPI msg\n");
} else {
indio_dev->info = &ad4000_info;
indio_dev->channels = chip->chan_spec;
indio_dev->num_channels = ARRAY_SIZE(chip->chan_spec);
}
ret = ad4000_prepare_3wire_mode_message(st, &indio_dev->channels[0]); if (ret) return dev_err_probe(dev, ret, "Failed to optimize SPI msg\n");
break; case AD4000_SDI_CS: if (st->using_offload) return dev_err_probe(dev, -EPROTONOSUPPORT, "Unsupported sdi-pin + offload config\n");
indio_dev->info = &ad4000_info;
indio_dev->channels = chip->chan_spec;
indio_dev->num_channels = ARRAY_SIZE(chip->chan_spec);
ret = ad4000_prepare_4wire_mode_message(st, &indio_dev->channels[0]); if (ret) return dev_err_probe(dev, ret, "Failed to optimize SPI msg\n");
break; case AD4000_SDI_GND: return dev_err_probe(dev, -EPROTONOSUPPORT, "Unsupported connection mode\n");
ret = devm_mutex_init(dev, &st->lock); if (ret) return ret;
st->gain_milli = 1000; if (chip->has_hardware_gain) {
ret = device_property_read_u16(dev, "adi,gain-milli",
&st->gain_milli); if (!ret) { /* Match gain value from dt to one of supported gains */
gain_idx = find_closest(st->gain_milli, ad4000_gains,
ARRAY_SIZE(ad4000_gains));
st->gain_milli = ad4000_gains[gain_idx];
} else { return dev_err_probe(dev, ret, "Failed to read gain property\n");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.