// SPDX-License-Identifier: GPL-2.0-only /* * Support code for Analog Devices Sigma-Delta ADCs * * Copyright 2012 Analog Devices Inc. * Author: Lars-Peter Clausen <lars@metafoo.de>
*/
/** * ad_sd_set_comm() - Set communications register * * @sigma_delta: The sigma delta device * @comm: New value for the communications register
*/ void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, u8 comm)
{ /* Some variants use the lower two bits of the communications register
* to select the channel */
sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK;
}
EXPORT_SYMBOL_NS_GPL(ad_sd_set_comm, "IIO_AD_SIGMA_DELTA");
/** * ad_sd_write_reg() - Write a register * * @sigma_delta: The sigma delta device * @reg: Address of the register * @size: Size of the register (0-3) * @val: Value to write to the register * * Returns 0 on success, an error code otherwise.
**/ int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsignedint reg, unsignedint size, unsignedint val)
{
u8 *data = sigma_delta->tx_buf; struct spi_transfer t = {
.tx_buf = data,
.len = size + 1,
.cs_change = sigma_delta->keep_cs_asserted,
}; struct spi_message m; int ret;
if (sigma_delta->bus_locked)
ret = spi_sync_locked(sigma_delta->spi, &m); else
ret = spi_sync(sigma_delta->spi, &m);
return ret;
}
/** * ad_sd_read_reg() - Read a register * * @sigma_delta: The sigma delta device * @reg: Address of the register * @size: Size of the register (1-4) * @val: Read value * * Returns 0 on success, an error code otherwise.
**/ int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta, unsignedint reg, unsignedint size, unsignedint *val)
{ int ret;
ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->rx_buf); if (ret < 0) goto out;
switch (size) { case 4:
*val = get_unaligned_be32(sigma_delta->rx_buf); break; case 3:
*val = get_unaligned_be24(sigma_delta->rx_buf); break; case 2:
*val = get_unaligned_be16(sigma_delta->rx_buf); break; case 1:
*val = sigma_delta->rx_buf[0]; break; default:
ret = -EINVAL; break;
}
/* * Read R̅D̅Y̅ pin (if possible) or status register to check if there is an * old event.
*/ if (sigma_delta->rdy_gpiod) {
pending_event = gpiod_get_value(sigma_delta->rdy_gpiod);
} else { unsignedint status_reg;
ret = ad_sd_read_reg(sigma_delta, AD_SD_REG_STATUS, 1, &status_reg); if (ret) return ret;
/* * In general the size of the data register is unknown. It varies from * device to device, might be one byte longer if CONTROL.DATA_STATUS is * set and even varies on some devices depending on which input is * selected. So send one byte to start reading the data register and * then just clock for some bytes with DIN (aka MOSI) high to not * confuse the register access state machine after the data register was * completely read. Note however that the sequence length must be * shorter than the reset procedure.
*/
data = kzalloc(data_read_len + 1, GFP_KERNEL); if (!data) return -ENOMEM;
spi_message_init(&m); if (sigma_delta->info->has_registers) { unsignedint data_reg = sigma_delta->info->data_reg ?: AD_SD_REG_DATA;
/* * The first transferred byte is part of the real data register, * so this doesn't need to be 0xff. In the remaining * `data_read_len - 1` bytes are less than $num_resetclks ones.
*/
t[1].tx_buf = data + 1;
data[1] = 0x00;
memset(data + 2, 0xff, data_read_len - 1);
spi_message_add_tail(&t[1], &m);
ret = spi_sync_locked(sigma_delta->spi, &m);
kfree(data);
return ret;
}
int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta, unsignedint mode, unsignedint channel)
{ int ret; unsignedlong time_left;
ret = ad_sigma_delta_set_channel(sigma_delta, channel); if (ret) return ret;
/** * ad_sd_calibrate_all() - Performs channel calibration * @sigma_delta: The sigma delta device * @cb: Array of channels and calibration type to perform * @n: Number of items in cb * * Returns 0 on success, an error code otherwise.
**/ int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta, conststruct ad_sd_calib_data *cb, unsignedint n)
{ unsignedint i; int ret;
for (i = 0; i < n; i++) {
ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel); if (ret) return ret;
}
/** * ad_sigma_delta_single_conversion() - Performs a single data conversion * @indio_dev: The IIO device * @chan: The conversion is done for this channel * @val: Pointer to the location where to store the read value * * Returns: 0 on success, an error value otherwise.
*/ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev, conststruct iio_chan_spec *chan, int *val)
{ struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); unsignedint sample, raw_sample; unsignedint data_reg; int ret = 0;
if (!iio_device_claim_direct(indio_dev)) return -EBUSY;
ret = ad_sigma_delta_set_channel(sigma_delta, chan->address); if (ret) goto out_release;
reg_size = BITS_TO_BYTES(scan_type->realbits + scan_type->shift); /* For 24-bit data, there is an extra byte of padding. */
status_pos = reg_size + (reg_size == 3 ? 1 : 0);
ret = spi_sync_locked(sigma_delta->spi, &sigma_delta->sample_msg); if (ret) goto irq_handled;
/* * For devices sampling only one channel at * once, there is no need for sample number tracking.
*/ if (sigma_delta->active_slots == 1) {
iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp); goto irq_handled;
}
if (sigma_delta->status_appended) {
u8 converted_channel;
converted_channel = data[status_pos] & sigma_delta->info->status_ch_mask; if (converted_channel != sigma_delta->slots[sigma_delta->current_slot]) { /* * Desync occurred during continuous sampling of multiple channels. * Drop this incomplete sample and start from first channel again.
*/
/* * AD7124 and a few others use the same physical line for interrupt * reporting (R̅D̅Y̅) and MISO. * As MISO toggles when reading a register, this likely results in a * pending interrupt. This has two consequences: a) The irq might * trigger immediately after it's enabled even though the conversion * isn't done yet; and b) checking the STATUS register's R̅D̅Y̅ flag is * off-limits as reading that would trigger another irq event. * * So read the MOSI line as GPIO (if available) and only trigger the irq * if the line is active. Without such a GPIO assume this is a valid * interrupt. * * Also as disable_irq_nosync() is used to disable the irq, only act if * the irq wasn't disabled before.
*/ if ((!sigma_delta->rdy_gpiod || gpiod_get_value(sigma_delta->rdy_gpiod)) &&
ad_sd_disable_irq(sigma_delta)) {
complete(&sigma_delta->completion); if (sigma_delta->trig)
iio_trigger_poll(sigma_delta->trig);
return IRQ_HANDLED;
}
return IRQ_NONE;
}
/** * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices * @indio_dev: The IIO device * @trig: The new trigger * * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta * device, -EINVAL otherwise.
*/ int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
{ struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
/** * devm_ad_sd_setup_buffer_and_trigger() - Device-managed buffer & trigger setup * @dev: Device object to which to bind the life-time of the resources attached * @indio_dev: The IIO device
*/ int devm_ad_sd_setup_buffer_and_trigger(struct device *dev, struct iio_dev *indio_dev)
{ struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); int ret;
sigma_delta->slots = devm_kcalloc(dev, sigma_delta->num_slots, sizeof(*sigma_delta->slots), GFP_KERNEL); if (!sigma_delta->slots) return -ENOMEM;
if (ad_sigma_delta_has_spi_offload(sigma_delta)) { struct dma_chan *rx_dma;
rx_dma = devm_spi_offload_rx_stream_request_dma_chan(dev,
sigma_delta->offload); if (IS_ERR(rx_dma)) return dev_err_probe(dev, PTR_ERR(rx_dma), "Failed to get RX DMA channel\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, "Cannot setup DMA buffer\n");
indio_dev->setup_ops = &ad_sd_buffer_setup_ops;
} else {
ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
&iio_pollfunc_store_time,
&ad_sd_trigger_handler,
&ad_sd_buffer_setup_ops); if (ret) return ret;
}
/** * ad_sd_init() - Initializes a ad_sigma_delta struct * @sigma_delta: The ad_sigma_delta device * @indio_dev: The IIO device which the Sigma Delta device is used for * @spi: The SPI device for the ad_sigma_delta device * @info: Device specific callbacks and options * * This function needs to be called before any other operations are performed on * the ad_sigma_delta struct.
*/ int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev, struct spi_device *spi, conststruct ad_sigma_delta_info *info)
{
sigma_delta->spi = spi;
sigma_delta->info = info;
/* If the field is unset in ad_sigma_delta_info, assume there can only be 1 slot. */ if (!info->num_slots)
sigma_delta->num_slots = 1; else
sigma_delta->num_slots = info->num_slots;
if (sigma_delta->num_slots > 1) { if (!indio_dev->info->update_scan_mode) {
dev_err(&spi->dev, "iio_dev lacks update_scan_mode().\n"); return -EINVAL;
}
if (info->has_named_irqs) {
sigma_delta->irq_line = fwnode_irq_get_byname(dev_fwnode(&spi->dev), "rdy"); if (sigma_delta->irq_line < 0) return dev_err_probe(&spi->dev, sigma_delta->irq_line, "Interrupt 'rdy' is required\n");
} else {
sigma_delta->irq_line = spi->irq;
}
sigma_delta->rdy_gpiod = devm_gpiod_get_optional(&spi->dev, "rdy", GPIOD_IN); if (IS_ERR(sigma_delta->rdy_gpiod)) return dev_err_probe(&spi->dev, PTR_ERR(sigma_delta->rdy_gpiod), "Failed to find rdy gpio\n");
if (sigma_delta->rdy_gpiod && !sigma_delta->irq_line) {
sigma_delta->irq_line = gpiod_to_irq(sigma_delta->rdy_gpiod); if (sigma_delta->irq_line < 0) return sigma_delta->irq_line;
}
if (info->supports_spi_offload) { struct spi_offload_config offload_config = {
.capability_flags = SPI_OFFLOAD_CAP_TRIGGER |
SPI_OFFLOAD_CAP_RX_STREAM_DMA,
}; int ret;
sigma_delta->offload = devm_spi_offload_get(&spi->dev, spi,
&offload_config);
ret = PTR_ERR_OR_ZERO(sigma_delta->offload); if (ret && ret != -ENODEV) return dev_err_probe(&spi->dev, ret, "Failed to get SPI offload\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.