Skip to content

Commit 0d9409f

Browse files
committed
iio: adc: ad7768-1: add support for Synchronization over SPI
The synchronization method using GPIO requires the generated pulse to be truly synchronous with the base MCLK signal. When it is not possible to do that in hardware, the datasheet recommends using synchronization over SPI, where the generated pulse is already synchronous with MCLK. This requires the SYNC_OUT pin to be connected to SYNC_IN pin. Add the option to handle device synchronization over SPI. Signed-off-by: Jonathan Santos <Jonathan.Santos@analog.com>
1 parent 97bfb17 commit 0d9409f

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

drivers/iio/adc/ad7768-1.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ struct ad7768_state {
255255
const char *labels[ARRAY_SIZE(ad7768_channels)];
256256
struct gpio_desc *gpio_reset;
257257
bool spi_is_dma_mapped;
258+
bool en_spi_sync;
258259
int irq;
259260
/*
260261
* DMA (thus cache coherency maintenance) may require the
@@ -323,6 +324,19 @@ static int ad7768_spi_reg_write_masked(struct ad7768_state *st,
323324
return ad7768_spi_reg_write(st, addr, (reg_val & ~mask) | val);
324325
}
325326

327+
static int ad7768_send_sync_pulse(struct ad7768_state *st)
328+
{
329+
if (st->en_spi_sync)
330+
return ad7768_spi_reg_write(st, AD7768_REG_SYNC_RESET, 0x00);
331+
332+
if (st->gpio_sync_in) {
333+
gpiod_set_value_cansleep(st->gpio_sync_in, 1);
334+
gpiod_set_value_cansleep(st->gpio_sync_in, 0);
335+
}
336+
337+
return 0;
338+
}
339+
326340
static int ad7768_set_mode(struct ad7768_state *st,
327341
enum ad7768_conv_mode mode)
328342
{
@@ -410,10 +424,7 @@ static int ad7768_set_dig_fil(struct ad7768_state *st,
410424
return ret;
411425

412426
/* A sync-in pulse is required every time the filter dec rate changes */
413-
gpiod_set_value(st->gpio_sync_in, 1);
414-
gpiod_set_value(st->gpio_sync_in, 0);
415-
416-
return 0;
427+
return ad7768_send_sync_pulse(st);
417428
}
418429

419430
int ad7768_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
@@ -732,11 +743,21 @@ static int ad7768_setup(struct ad7768_state *st)
732743
if (ret)
733744
return ret;
734745

735-
st->gpio_sync_in = devm_gpiod_get(&st->spi->dev, "adi,sync-in",
746+
st->gpio_sync_in = devm_gpiod_get_optional(&st->spi->dev, "adi,sync-in",
736747
GPIOD_OUT_LOW);
737748
if (IS_ERR(st->gpio_sync_in))
738749
return PTR_ERR(st->gpio_sync_in);
739750

751+
if (device_property_present(&st->spi->dev, "adi,sync-in-spi"))
752+
st->en_spi_sync = true;
753+
754+
/*
755+
* GPIO and SPI Synchronization are mutually exclusive.
756+
* Return error if both are enabled
757+
*/
758+
if (st->gpio_sync_in && st->en_spi_sync)
759+
return -EINVAL;
760+
740761
ret = ad7768_gpio_init(st);
741762
if (ret < 0)
742763
return ret;

0 commit comments

Comments
 (0)