Skip to content

Commit fe61aed

Browse files
dlechnunojsa
authored andcommitted
iio: adc: ad7944: revert ADI tree changed
Revert all ADI tree changes to the ad7944 driver compared to the mainline kernel as of commit 6020ca4 ("iio: adc: ad7944: use spi_optimize_message()"). This will allow us to cherry-pick mainline changes on top of the driver to be able to use the mainline version of SPI offload support. Signed-off-by: David Lechner <dlechner@baylibre.com>
1 parent 7a52550 commit fe61aed

File tree

1 file changed

+12
-236
lines changed

1 file changed

+12
-236
lines changed

drivers/iio/adc/ad7944.c

Lines changed: 12 additions & 236 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,22 @@
1414
#include <linux/gpio/consumer.h>
1515
#include <linux/module.h>
1616
#include <linux/property.h>
17-
#include <linux/pwm.h>
1817
#include <linux/regulator/consumer.h>
19-
#include <linux/spi/spi-engine-ex.h>
2018
#include <linux/spi/spi.h>
2119
#include <linux/string_helpers.h>
2220

2321
#include <linux/iio/iio.h>
2422
#include <linux/iio/sysfs.h>
25-
#include <linux/iio/buffer-dmaengine.h>
2623
#include <linux/iio/trigger_consumer.h>
2724
#include <linux/iio/triggered_buffer.h>
2825

29-
/*
30-
* Older SPI Engine HDL versions have a bug where the offload is level triggered
31-
* instead of edge triggered, so we use a small duty cycle to allow sampling
32-
* at lower rates without spurious triggers.
33-
*/
34-
#define AD7944_PWM_TRIGGER_DUTY_CYCLE_NS 20
35-
#define AD7944_DEFAULT_SAMPLE_FREQ_HZ 10000 /* arbitrary */
3626
#define AD7944_INTERNAL_REF_MV 4096
3727

3828
struct ad7944_timing_spec {
3929
/* Normal mode max conversion time (t_{CONV}). */
4030
unsigned int conv_ns;
4131
/* TURBO mode max conversion time (t_{CONV}). */
4232
unsigned int turbo_conv_ns;
43-
/* Normal mode data read during conversion time (t_{DATA}). */
44-
unsigned int data_ns;
45-
/* TURBO mode data read during conversion time (t_{DATA}). */
46-
unsigned int turbo_data_ns;
4733
};
4834

4935
enum ad7944_spi_mode {
@@ -67,8 +53,6 @@ struct ad7944_adc {
6753
enum ad7944_spi_mode spi_mode;
6854
struct spi_transfer xfers[3];
6955
struct spi_message msg;
70-
struct spi_transfer turbo_xfers[3];
71-
struct spi_message turbo_msg;
7256
/* Chip-specific timing specifications. */
7357
const struct ad7944_timing_spec *timing_spec;
7458
/* GPIO connected to CNV pin. */
@@ -79,8 +63,6 @@ struct ad7944_adc {
7963
bool always_turbo;
8064
/* Reference voltage (millivolts). */
8165
unsigned int ref_mv;
82-
/* SPI offload trigger. */
83-
struct pwm_device *pwm;
8466

8567
/*
8668
* DMA (thus cache coherency maintenance) requires the
@@ -95,32 +77,23 @@ struct ad7944_adc {
9577
} sample __aligned(IIO_DMA_MINALIGN);
9678
};
9779

98-
/* minimum CNV high time */
99-
#define T_CNVH_NS 10
100-
/* CS low to data valid time */
101-
#define T_EN_NS 5
10280
/* quite time before CNV rising edge */
10381
#define T_QUIET_NS 20
10482

10583
static const struct ad7944_timing_spec ad7944_timing_spec = {
10684
.conv_ns = 420,
10785
.turbo_conv_ns = 320,
108-
.data_ns = 290,
109-
.turbo_data_ns = 190,
11086
};
11187

11288
static const struct ad7944_timing_spec ad7986_timing_spec = {
11389
.conv_ns = 500,
11490
.turbo_conv_ns = 400,
115-
.data_ns = 300,
116-
.turbo_data_ns = 200,
11791
};
11892

11993
struct ad7944_chip_info {
12094
const char *name;
12195
const struct ad7944_timing_spec *timing_spec;
12296
const struct iio_chan_spec channels[2];
123-
const struct iio_chan_spec offload_channels[1];
12497
};
12598

12699
/*
@@ -151,23 +124,6 @@ static const struct ad7944_chip_info _name##_chip_info = { \
151124
}, \
152125
IIO_CHAN_SOFT_TIMESTAMP(1), \
153126
}, \
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-
}, \
171127
}
172128

173129
/* pseudo-differential with ground sense */
@@ -208,11 +164,11 @@ static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *
208164
xfers[1].cs_off = 1;
209165
xfers[1].delay.value = t_conv_ns;
210166
xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS;
211-
xfers[0].bits_per_word = chan->scan_type.realbits;
167+
xfers[1].bits_per_word = chan->scan_type.realbits;
212168

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

218174
spi_message_init_with_transfers(&adc->msg, xfers, 3);
@@ -224,57 +180,6 @@ static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *
224180
return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg);
225181
}
226182

227-
static int ad7944_3wire_cs_mode_init_turbo_msg(struct device *dev,
228-
struct ad7944_adc *adc,
229-
const struct iio_chan_spec *chan)
230-
{
231-
struct spi_transfer *xfers = adc->turbo_xfers;
232-
int ret;
233-
234-
/*
235-
* This sequence of xfers performs a read during conversion in 3-wire CS
236-
* mode with timings for when TURBO mode is enabled. Using this msg will
237-
* only work with SPI offloads since the timing needs to be precise to
238-
* actually be able to read during the conversion time.
239-
*/
240-
241-
/* change CNV to high to trigger conversion */
242-
xfers[0].cs_off = 1;
243-
xfers[0].bits_per_word = chan->scan_type.realbits;
244-
xfers[0].delay.value = T_CNVH_NS;
245-
xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
246-
247-
/* read sample data from previous conversion */
248-
xfers[1].rx_buf = &adc->sample.raw;
249-
xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;
250-
xfers[1].bits_per_word = chan->scan_type.realbits;
251-
/*
252-
* CNV has to be high at end of conversion to avoid triggering the busy
253-
* signal on the SDO line.
254-
*/
255-
xfers[1].cs_change = 1;
256-
/* t[CONV] - t[CNVH] - t[EN] - t[DATA] */
257-
xfers[1].cs_change_delay.value = adc->timing_spec->turbo_conv_ns -
258-
T_CNVH_NS - T_EN_NS -
259-
adc->timing_spec->turbo_data_ns;
260-
xfers[1].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
261-
262-
/*
263-
* Since this is the last xfer, this changes CNV to low and keeps it
264-
* there until the next xfer.
265-
*/
266-
xfers[2].cs_change = 1;
267-
xfers[2].bits_per_word = chan->scan_type.realbits;
268-
269-
spi_message_init_with_transfers(&adc->turbo_msg, xfers, 3);
270-
271-
ret = spi_optimize_message(adc->spi, &adc->turbo_msg);
272-
if (ret)
273-
return ret;
274-
275-
return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->turbo_msg);
276-
}
277-
278183
static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
279184
const struct iio_chan_spec *chan)
280185
{
@@ -297,10 +202,10 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
297202
xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
298203

299204
xfers[1].rx_buf = &adc->sample.raw;
300-
xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;
205+
xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
301206
xfers[1].bits_per_word = chan->scan_type.realbits;
302207

303-
spi_message_init_with_transfers(&adc->msg, xfers, 3);
208+
spi_message_init_with_transfers(&adc->msg, xfers, 2);
304209

305210
ret = spi_optimize_message(adc->spi, &adc->msg);
306211
if (ret)
@@ -374,7 +279,7 @@ static int ad7944_single_conversion(struct ad7944_adc *adc,
374279
return -EOPNOTSUPP;
375280
}
376281

377-
if (chan->scan_type.realbits > 16)
282+
if (chan->scan_type.storagebits > 16)
378283
*val = adc->sample.raw.u32;
379284
else
380285
*val = adc->sample.raw.u16;
@@ -417,110 +322,13 @@ static int ad7944_read_raw(struct iio_dev *indio_dev,
417322
return -EINVAL;
418323
}
419324

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-
428-
default:
429-
return -EINVAL;
430-
}
431-
}
432-
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)
436-
{
437-
struct ad7944_adc *adc = iio_priv(indio_dev);
438-
439-
switch (info) {
440-
case IIO_CHAN_INFO_SAMP_FREQ: {
441-
struct pwm_state state;
442-
443-
if (val < 0)
444-
return -EINVAL;
445-
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;
450-
451-
return pwm_apply_state(adc->pwm, &state);
452-
}
453325
default:
454326
return -EINVAL;
455327
}
456328
}
457329

458-
static int ad7944_write_raw_get_fmt(struct iio_dev *indio_dev,
459-
const struct iio_chan_spec *chan,
460-
long info)
461-
{
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-
}
468-
}
469-
470330
static const struct iio_info ad7944_iio_info = {
471331
.read_raw = &ad7944_read_raw,
472-
.write_raw = &ad7944_write_raw,
473-
.write_raw_get_fmt = &ad7944_write_raw_get_fmt,
474-
};
475-
476-
static int ad7944_offload_ex_buffer_preenable(struct iio_dev *indio_dev)
477-
{
478-
struct ad7944_adc *adc = iio_priv(indio_dev);
479-
480-
switch (adc->spi_mode) {
481-
case AD7944_SPI_MODE_DEFAULT:
482-
return spi_engine_ex_offload_load_msg(adc->spi, &adc->msg);
483-
case AD7944_SPI_MODE_SINGLE:
484-
/* REVISIT: could do non-turbo for lower sample rates */
485-
gpiod_set_value_cansleep(adc->turbo, 1);
486-
return spi_engine_ex_offload_load_msg(adc->spi, &adc->turbo_msg);
487-
default:
488-
return -EOPNOTSUPP;
489-
}
490-
}
491-
492-
static int ad7944_offload_ex_buffer_postenable(struct iio_dev *indio_dev)
493-
{
494-
struct ad7944_adc *adc = iio_priv(indio_dev);
495-
496-
spi_engine_ex_offload_enable(adc->spi, true);
497-
498-
return 0;
499-
}
500-
501-
static int ad7944_offload_ex_buffer_predisable(struct iio_dev *indio_dev)
502-
{
503-
struct ad7944_adc *adc = iio_priv(indio_dev);
504-
505-
spi_engine_ex_offload_enable(adc->spi, false);
506-
507-
return 0;
508-
}
509-
510-
static int ad7944_offload_ex_buffer_postdisable(struct iio_dev *indio_dev)
511-
{
512-
struct ad7944_adc *adc = iio_priv(indio_dev);
513-
514-
gpiod_set_value_cansleep(adc->turbo, 0);
515-
516-
return 0;
517-
}
518-
519-
static const struct iio_buffer_setup_ops ad7944_offload_ex_buffer_setup_ops = {
520-
.preenable = &ad7944_offload_ex_buffer_preenable,
521-
.postenable = &ad7944_offload_ex_buffer_postenable,
522-
.predisable = &ad7944_offload_ex_buffer_predisable,
523-
.postdisable = &ad7944_offload_ex_buffer_postdisable,
524332
};
525333

526334
static irqreturn_t ad7944_trigger_handler(int irq, void *p)
@@ -705,10 +513,6 @@ static int ad7944_probe(struct spi_device *spi)
705513
if (ret)
706514
return ret;
707515

708-
ret = ad7944_3wire_cs_mode_init_turbo_msg(dev, adc, &chip_info->channels[0]);
709-
if (ret)
710-
return ret;
711-
712516
break;
713517
case AD7944_SPI_MODE_CHAIN:
714518
return dev_err_probe(dev, -EINVAL, "chain mode is not implemented\n");
@@ -717,42 +521,14 @@ static int ad7944_probe(struct spi_device *spi)
717521
indio_dev->name = chip_info->name;
718522
indio_dev->modes = INDIO_DIRECT_MODE;
719523
indio_dev->info = &ad7944_iio_info;
524+
indio_dev->channels = chip_info->channels;
525+
indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
720526

721-
if (spi_engine_ex_offload_supported(spi)) {
722-
struct pwm_state state = {
723-
.period = NSEC_PER_SEC / AD7944_DEFAULT_SAMPLE_FREQ_HZ,
724-
.duty_cycle = AD7944_PWM_TRIGGER_DUTY_CYCLE_NS,
725-
.enabled = true,
726-
};
727-
728-
indio_dev->channels = chip_info->offload_channels;
729-
indio_dev->num_channels = ARRAY_SIZE(chip_info->offload_channels);
730-
indio_dev->setup_ops = &ad7944_offload_ex_buffer_setup_ops;
731-
732-
adc->pwm = devm_pwm_get(dev, NULL);
733-
if (IS_ERR(adc->pwm))
734-
return dev_err_probe(dev, PTR_ERR(adc->pwm),
735-
"failed to get PWM\n");
736-
737-
ret = pwm_apply_state(adc->pwm, &state);
738-
if (ret)
739-
return dev_err_probe(dev, ret,
740-
"failed to apply PWM state\n");
741-
742-
ret = devm_iio_dmaengine_buffer_setup(dev, indio_dev, "rx");
743-
if (ret)
744-
return ret;
745-
} else {
746-
indio_dev->channels = chip_info->channels;
747-
indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
748-
749-
ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
750-
iio_pollfunc_store_time,
751-
ad7944_trigger_handler,
752-
NULL);
753-
if (ret)
754-
return ret;
755-
}
527+
ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
528+
iio_pollfunc_store_time,
529+
ad7944_trigger_handler, NULL);
530+
if (ret)
531+
return ret;
756532

757533
return devm_iio_device_register(dev, indio_dev);
758534
}

0 commit comments

Comments
 (0)